【问题标题】:Nested Attributes and Unpermitted parameters嵌套属性和不允许的参数
【发布时间】:2016-02-06 23:12:04
【问题描述】:

我有一个传递嵌套属性的表单,它适用于“孩子”,但“书籍”似乎没有保存。

我有 3 个模型。每个孩子可以有 2 本书:

class Child < ActiveRecord::Base
 has_many :hires
 has_many :books, through: :hires
end

class Hire < ActiveRecord::Base
 belongs_to :book
 belongs_to :child
 accepts_nested_attributes_for :book
 accepts_nested_attributes_for :child
end

class Book < ActiveRecord::Base
  has_many :hires
  has_many :children, through: :hires
  belongs_to :genres
end

控制器如下所示:

class HiresController < ApplicationController

...    

 def new
     @hire = Hire.new
     2.times { @hire.build_book }
 end

 def create
    @hire = Hire.new(hire_params)

    respond_to do |format|
      if @hire.save
        format.html { redirect_to @hire, notice: 'Hire was successfully created.' }
        format.json { render :show, status: :created, location: @hire }
      else
        format.html { render :new }
        format.json { render json: @hire.errors, status: :unprocessable_entity }
      end
    end
  end

 ...    

private
    # Use callbacks to share common setup or constraints between actions.
    def set_hire
      @hire = Hire.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
        def hire_params
  params.require(:hire).permit(:child_id, books_attributes: [ :id, :book_id, :_destroy])
end
end

正在提交的哈希如下所示:

Processing by HiresController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"+2xxx==", "hire"=>{"child_id"=>"2", "books"=>{"book_id"=>"5"}}, "commit"=>"Create Hire"}
Unpermitted parameter: books

我不知道为什么会出现未经许可的参数错误?有什么建议吗?

* 编辑 - 包括视图,因为我现在怀疑这可能会起作用 *

<%= form_for(@hire) do |f| %>

<%= f.select(:child_id, Child.all.collect {|a| [a.nickname, a.id]}) -%>
<%= f.label :child_id %><br>

<%= f.fields_for :books do |books_form| %>
<%= books_form.label :book_id %><br>
<%= books_form.select(:book_id, Book.all.collect {|a| [a.Title, a.id]}) -%>
<%= books_form.label :book_id %><br>
<%= books_form.select(:book_id, Book.all.collect {|a| [a.Title, a.id]}) -%>
<% end %>


<div class="actions">
<%= f.submit %>
</div>
<% end %>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 nested-attributes


    【解决方案1】:

    您需要使用以下内容:

    #app/controllers/hires_controller.rb
    class HiresController < ApplicationController
       def new
          @hire = Hire.new
          2.times do
             @hire.build_book
          end
       end
    end
    

    其余的看起来还可以。

    你遇到的问题很典型;当您使用f.fields_for 时,您必须构建关联对象,否则fields_for 将无法创建正确的字段名称。

    您要查找的字段名称将是 [books_attributes][0][book_id] 等。


    由于您的表单传递了您期望的所有属性(没有_attributes 后缀),我只能推测您构建的关联对象不正确:

    2.times { @hire.books.build }
    

    ...应该是...

    2.times { @hire.build_book }
    

    当您有单数关联时,您必须单独构建关联对象:build_object,而对于复数,您可以使用 plural.build 调用。

    【讨论】:

    • 嗨 Rich,谢谢。我想你是正确的。某些东西似乎正在影响属性的构建。我将控制器代码更改为2.times do @hire.build_book end,但仍然遇到同样的问题。还有其他想法吗?
    • 我想知道这是否与我的联想有关。我说的是`accepts_nested_attributes_for :book`,但我的f.fieldsbooks,我的属性是books
    • 去买点吃的去看看
    猜你喜欢
    • 2013-04-01
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    相关资源
    最近更新 更多