Rails - 在Cocoon中使用嵌套表单时未保存参数

时间:2014-11-18 14:09:46

标签: ruby-on-rails forms parameters nested-forms cocoon-gem

我已经按照Cocoon的说明为has_many协会创建了一个嵌套表单,但有一件事我似乎无法弄明白。我能够为我的项目添加一个现有的类型,但是当创建一个全新的类型时,我的类型的参数是不被允许的。当我引用我的rails控制台调试问题时,这是我在更新时收到的消息:

Unpermitted parameters: genre_attributes

奇怪的是,我允许我的联接表genreships和我的genre模型以及我的movie模型接受其嵌套属性但仍然没有运气。我必须在我的movie_params方法中做一些事情,但却无法找到。我真的试图在这种方法中允许所有可能的属性,完全没有关于可能解决方案的想法

模型

class Movie < ActiveRecord::Base
    has_many :genreships
    has_many :genres, through: :genreships

    accepts_nested_attributes_for :genres, :reject_if => :all_blank, :allow_destroy => true
    accepts_nested_attributes_for :genreships, :reject_if => :all_blank, :allow_destroy => true

    extend FriendlyId
    friendly_id :title, use: :slugged
end

class Genreship < ActiveRecord::Base
    belongs_to :movie
    belongs_to :genre

    accepts_nested_attributes_for :genre, :reject_if => :all_blank
end

class Genre < ActiveRecord::Base
    has_many :genreships
    has_many :movies, through: :genreships

    extend FriendlyId
    friendly_id :name, use: :slugged
end

电影控制器

def movie_params
  params.require(:movie).permit(:title, :release_date, :summary, genreships_attributes: [:id, :genre_id, :_destroy], genres_attributes: [:id, :_destroy, :name])
end

表格

= simple_form_for @movie do |f|

  .field
    = f.input :title
  .field
    = f.input :release_date, label: 'Release Date', order: [:month, :day, :year], start_year: 1901
  .field
    = f.input :summary, as: :text
  #genres
    = f.simple_fields_for :genreships do |genreship|
      = render 'genreship_fields', :f => genreship
    = link_to_add_association 'add a genre', f, :genreships
  .actions = f.submit 'Save', class: 'btn btn-default'

Genreship Partial

.nested-fields
  #genre_from_list
    = f.association :genre, :collection => Genre.order(:name), :prompt => 'Choose an existing genre'
  = link_to_add_association 'or create a new genre', f, :genre
  = link_to_remove_association "remove genre", f

流派偏见

.nested-fields
  = f.input :name

以下是发布新电影后在rails控制台中显示的内容:

Started POST "/movies" for 127.0.0.1 at 2014-11-19 08:17:42 -0500

Processing by MoviesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"2/wqXhn/x73AdOOrSo49Q/OPAmrcVk0rLViJJNpIhps=", "movie"=>{"title"=>"", "release_date(2i)"=>"11", "release_date(3i)"=>"19", "release_date(1i)"=>"2014", "summary"=>"", "genreships_attributes"=>{"1416403056574"=>{"genre_attributes"=>{"name"=>"Comedy"}, "genre_id"=>"", "_destroy"=>"false"}}}, "commit"=>"Save"}
Unpermitted parameters: genre_attributes

2 个答案:

答案 0 :(得分:0)

您使用genre_attributes,而控制器中定义了genres_attributes(复数形式)。将其更改为:

def movie_params
  params.require(:movie).permit(:title, :release_date, :summary, genreships_attributes: [:id, :genre_id, :_destroy], genre_attributes: [:id, :_destroy, :name])
end

答案 1 :(得分:0)

我创建了一个解决方案,在某种程度上偏离了Nathan的示例代码,但仍然完成了工作。只需将此行从genreship_fields.html.slim移至_form.html.slim即可:

link_to_add_association 'create a new genre', f, :genres

我的偏见现在看起来像这样:

<强>表格

= simple_form_for @movie do |f|
  .field
    = f.input :title
  .field
    = f.input :release_date, label: 'Release Date', order: [:month, :day, :year], start_year: 1901
  .field
    = f.input :summary, as: :text
  #genres
    = f.simple_fields_for :genreships do |genreship|
      = render 'genreship_fields', :f => genreship
    | #{link_to_add_association 'add a genre', f, :genreships} | #{link_to_add_association 'create a new genre', f, :genres}
  .actions = f.submit 'Save', class: 'btn btn-default'

Genreship Partial

.nested-fields
  #genre_from_list
    = f.association :genre, :collection => Genre.order(:name), :prompt => 'Choose an existing genre'
  = link_to_remove_association "remove genre", f

流派偏见

.nested-fields
  = f.input :name
  = link_to_remove_association "remove form", f