【发布时间】:2015-05-18 17:04:31
【问题描述】:
我有一个表格:
= 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
这里的关系是 Character 有很多 MovieCharacter 和 MovieCharacter belongs_to Movie。
我从这个表单中得到的参数如下所示:
{"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
不幸的是,在控制器中调用此方法时,我收到“Unpermitted parameter: 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
【问题讨论】:
-
试试
= f.simple_fields_for :movie_characters do |fmc| -
这对你不起作用?
-
不,它是一个更好的解决方案,但它不起作用,我更新了我的问题,它现在有你问的模型代码
-
@Leo- 模型代码看起来不错
-
@Sontya 我欠你一个道歉,我使用的是旧电脑并且没有我习惯的自动保存功能,你的第一个建议有效,我只是没有保存它。谢谢,麻烦了
标签: ruby-on-rails strong-parameters