(前言/免责声明:我想知道我应该学会什么以防止将来出现此类错误。我是编程新手,并且一直在阅读有关强壮参数的所有内容但它似乎没有解决问题)parent_params:更新嵌套属性
我有两个模型:艺术家(父母),和歌曲(孩子),has_many/belongs_to关系。我使用嵌套属性form_for @parent创建一个子对象,并将其所有代码放在parents_controller中。 (在User和Child之间也有一个连接表,在代码中称为“UserSong”)。
一切都很好创建,但是当我点击显示页面上的“编辑”为孩子,我得到一个窗体与所有的子对象,而不是我点击的具体子对象 - 不是我想要的,但主要问题是,当我继续编辑子对象并按下“提交”时,我得到一个NoMethodError in ParentsController#update
,指定private method ‘update’ called for nil:NilClass
,我似乎无法摆脱。
突出显示的代码行是if @parent.update(parent_params)
,我在下面的代码中以粗体显示。
parents_controller:
class ArtistsController < ApplicationController
before_action :authenticate_user!
def create
@artist = Artist.find_or_create_by(name: params[:artist][:name].strip.titleize)
@song = @artist.songs.find_or_create_by(title: params[:artist][:songs_attributes]["0"][:title].strip.titleize) do |song|
song.lyrics = params[:artist][:songs_attributes]["0"][:lyrics].strip
end
@user_song = current_user.user_songs.find_or_create_by(song_id: @song.id) do |user_id|
user_id.user_id = current_user.id
end
redirect_to root_path
end
def index
@songs = Song.all
end
def new
@artist = Artist.new
@artist.songs.build
@user_song = UserSong.new(user_id: current_user.id, song_id: @song)
end
def show
@song = Song.find(params[:id])
end
def destroy
UserSong.where(:song_id => params[:id]).first.destroy
flash[:success] = "The song has been removed from your playlist"
redirect_to root_path
end
def edit
@song = Song.find(params[:id])
@artist = @song.artist
end
def update
respond_to do |format|
if @artist.update(artist_params)
format.html { redirect_to session.delete(:return_to), notice: 'Song/Artist was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @artist.errors, status: :unprocessable_entity }
end
end
end
private
def artist_params
params.require(:artist).permit(:id, :name, songs_attributes: [:id, :title, :lyrics])
end
end
的形式如下:
<%= form_for @artist do |f| %>
<div class="form-group">
<%= f.text_field :name, placeholder: "Artist", class: "form-control" %>
</div>
<%= f.fields_for :songs do |p| %>
<div class="form-group">
<%= p.text_field :title, placeholder: "Song title", class: "form-control" %>
<br />
<%= p.text_area :lyrics, placeholder: "Input lyrics here", rows: 20, class: "form-control" %>
</div>
<% end %>
<div class="form-group">
<%= f.submit "Add song" %>
</div>
<% end %>
指着我在正确的方向任何线索将不胜感激!
编辑:如果有帮助,我已经发布了以下相关路线:
artists GET /artists(.:format) artists#index
POST /artists(.:format) artists#create
new_artist GET /artists/new(.:format) artists#new
edit_artist GET /artists/:id/edit(.:format) artists#edit
artist GET /artists/:id(.:format) artists#show
PATCH /artists/:id(.:format) artists#update
PUT /artists/:id(.:format) artists#update
DELETE /artists/:id(.:format) artists#destroy
songs GET /songs(.:format) artists#index
POST /songs(.:format) artists#create
new_song GET /songs/new(.:format) artists#new
edit_song GET /songs/:id/edit(.:format) artists#edit
song GET /songs/:id(.:format) artists#show
PATCH /songs/:id(.:format) artists#update
PUT /songs/:id(.:format) artists#update
DELETE /songs/:id(.:format) artists#destroy
这做到了你的男人在5分钟内解决调试两天..。我不确定我是否应该感到高兴,或者自己感到烦恼...... lol – michaelsking1993
Nine超过10个零错误的结果是初始化一个变量失败(Ruby的一个缺点)。 – chrismanderson
为什么这是一个缺点? – michaelsking1993