ruby on rails - NoMethodError when generating new view -
i'm working on making simple app has users can have many playlists. i'm trying render new view of playlist i'm getting error:
nomethoderror in playlistscontroller#new undefined method `playlist' nil:nilclass def new @playlist = @user.playlist.new end
here's context:
edit: uploaded relevant parts of code gist.github:
https://gist.github.com/izikperz/164eab76e64d375d9075
playlist_controller.rb
class playlistscontroller < applicationcontroller before_action :set_playlist, only: [:show, :edit, :update, :destroy] :set_user # /playlists # /playlists.json def index @playlists = playlist.all end # /playlists/1 # /playlists/1.json def show end # /playlists/new def new @playlist = @user.playlist.new end # /playlists/1/edit def edit end # post /playlists # post /playlists.json def create @playlist = @user.playlists.new(playlist_params) respond_to |format| if @playlist.save format.html { redirect_to @user.playlist, notice: 'playlist created.' } format.json { render :show, status: :created, location: @playlist } else format.html { render :new } #format.json { render json: @playlist.errors, status: :unprocessable_entity } end end end # patch/put /playlists/1 # patch/put /playlists/1.json def update respond_to |format| if @playlist.update(playlist_params) format.html { redirect_to @playlist, notice: 'playlist updated.' } format.json { render :show, status: :ok, location: @playlist } else format.html { render :edit } format.json { render json: @playlist.errors, status: :unprocessable_entity } end end end # delete /playlists/1 # delete /playlists/1.json def destroy @playlist.destroy respond_to |format| format.html { redirect_to playlists_url, notice: 'playlist destroyed.' } format.json { head :no_content } end end private def set_user @user = user.find_by(params[:user_id]) end # utilize callbacks share mutual setup or constraints between actions. def set_playlist @playlist = playlist.find(params[:id]) end # never trust parameters scary internet, allow white list through. def playlist_params params.require(:playlist).permit(:user_id, :title, :img) end end
in routes.rb have:
resources :users resources :playlists end
my user.rb model:
class user < activerecord::base before_save { self.email = email.downcase } has_secure_password validates :password, length: { minimum: 6 } has_many :playlists end
playlist.rb model:
class playlist < activerecord::base belongs_to :user, inverse_of: :playlist validates :user_id, presence: true end
my database schema:
activerecord::schema.define(version: 20141105043809) create_table "playlists", force: true |t| t.string "title" t.string "img" t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" end create_table "users", primary_key: "user_id", force: true |t| t.string "name" t.string "email" t.datetime "created_at" t.datetime "updated_at" t.string "password_digest" t.string "imgurl" end end
anyone have ideas?
the @user
object in new
action nil
. undefined method `playlist' nil:nilclass
this because params[:user_id] not sent view.
fix chaniging:
new_user_playlist_path(params[:user_id])
to:
new_user_playlist_path(user_id: params[:user_id])
after fixing that, error arise in new
action:
@playlist = @user.playlist.new
it should pluralized in association:
@playlist = @user.playlists.new #or @playlist = @user.playlists.build
or ignore that:
@playlist = playlist.new(user_id: @user.id)
ruby-on-rails model-view-controller controller
No comments:
Post a Comment