我有一张表格:
= simple_form_for @character do |f|
= f.input :name
= f.input :description
= f.simple_fields_for @movie_characters do |fmc|
= fmc.association :movie, value_method: :id, label_method: :title
= f.submit
这里的关系是角色有很多MovieCharacters和MovieCharacter属于电影。
我从这个表单得到的params看起来像这样:{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"6d8n1Z5AC3nF/cqzTInnMr5TyRhZFcGV8ssf5CsNkjskiD9lUL10nVDYeV4i0yS85Q87yNcIi+coFFDf0mpZ4w==", "character"=>{"name"=>"Joker", "description"=>"best villain ever", "movie_character"=>{"movie_id"=>"1"}}, "commit"=>"Update Character", "controller"=>"characters", "action"=>"update", "id"=>"1"}
character_params方法如下所示:
def character_params
params.require(:character).permit(:name, :description, movie_characters_attributes: [:movie_id, :character_id])
end
不幸的是,在控制器中调用此方法时,我收到“未经许可的参数:movie_character”警告。 我也试过
def character_params
params.require(:character).permit(:name, :description, movie_characters: [:movie_id, :character_id])
end
但效果是一样的。你能告诉我它为什么不起作用吗?
更新:
class Character < ActiveRecord::Base
has_many :movie_characters
accepts_nested_attributes_for :movie_characters
end
class MovieCharacter < ActiveRecord::Base
belongs_to :character
belongs_to :movie
end
答案 0 :(得分:0)
你有一个错字。它应该是:
def character_params
params.require(:character).permit(:name, :description, movie_character_attributes: [:movie_id, :character_id])
end
没有&#34; s&#34;在movie_characters_attributes中。