【问题标题】:Nested attributes unpermitted parameters嵌套属性不允许的参数
【发布时间】:2013-04-01 22:26:29
【问题描述】:

我有一个Bill 对象,其中有许多Due 对象。 Due 对象也属于 Person。我想要一个可以在一个页面中创建Bill 及其子Dues 的表单。我正在尝试使用嵌套属性创建表单,类似于this Railscast 中的属性。

相关代码如下:

due.rb

class Due < ActiveRecord::Base
    belongs_to :person
    belongs_to :bill
end

bill.rb

class Bill < ActiveRecord::Base
    has_many :dues, :dependent => :destroy 
    accepts_nested_attributes_for :dues, :allow_destroy => true
end

bills_controller.rb

  # GET /bills/new
  def new
      @bill = Bill.new
      3.times { @bill.dues.build }
  end

bills/_form.html.erb

  <%= form_for(@bill) do |f| %>
    <div class="field">
        <%= f.label :company %><br />
        <%= f.text_field :company %>
    </div>
    <div class="field">
        <%= f.label :month %><br />
        <%= f.text_field :month %>
    </div>
    <div class="field">
        <%= f.label :year %><br />
        <%= f.number_field :year %>
    </div>
    <div class="actions">
        <%= f.submit %>
    </div>
    <%= f.fields_for :dues do |builder| %>
        <%= render 'due_fields', :f => builder %>
    <% end %>
  <% end %>

bills/_due_fields.html.erb

<div>
    <%= f.label :amount, "Amount" %>        
    <%= f.text_field :amount %>
    <br>
    <%= f.label :person_id, "Renter" %>
    <%= f.text_field :person_id %>
</div>

更新 bills_controller.rb 这行得通!

def bill_params 
  params
  .require(:bill)
  .permit(:company, :month, :year, dues_attributes: [:amount, :person_id]) 
end

页面上呈现了正确的字段(尽管还没有Person 的下拉菜单)并且提交成功。但是,没有一个孩子的会费被保存到数据库中,并且在服务器日志中抛出了一个错误:

Unpermitted parameters: dues_attributes

就在错误发生之前,日志显示如下:

Started POST "/bills" for 127.0.0.1 at 2013-04-10 00:16:37 -0700
Processing by BillsController#create as HTML<br>
Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"ipxBOLOjx68fwvfmsMG3FecV/q/hPqUHsluBCPN2BeU=",
 "bill"=>{"company"=>"Comcast", "month"=>"April ", 
"year"=>"2013", "dues_attributes"=>{
"0"=>{"amount"=>"30", "person_id"=>"1"}, 
"1"=>{"amount"=>"30", "person_id"=>"2"},
 "2"=>{"amount"=>"30", "person_id"=>"3"}}}, "commit"=>"Create Bill"}

Rails 4 有什么变化吗?

【问题讨论】:

  • 修正格式:params.require(:bill).permit(:company, :month, :year, :dues_attributes => [:amount, :person_id])

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


【解决方案1】:

如果使用 JSONB 字段,则必须使用 .to_json (ROR) 将其转换为 JSON

【讨论】:

    【解决方案2】:

    似乎属性保护的处理发生了变化,现在您必须将控制器中的参数列入白名单(而不是模型中的 attr_accessible),因为以前的可选 gem strong_parameters 已成为 Rails 核心的一部分。

    这应该看起来像这样:

    class PeopleController < ActionController::Base
      def create
        Person.create(person_params)
      end
    
    private
      def person_params
        params.require(:person).permit(:name, :age)
      end
    end
    

    所以params.require(:model).permit(:fields) 会被使用

    对于嵌套属性类似于

    params.require(:person).permit(:name, :age, pets_attributes: [:id, :name, :category])
    

    更多细节可以在Ruby edge API docsstrong_parameters on githubhere中找到

    【讨论】:

    • 我已将 BillController 更改为如下所示:def bill_params params.require(:bill).permit(:company, :month, :year, :dues_attributes[:amount, :person_id]) end 我现在收到此错误:没有将 Symbol 隐式转换为 Integer
    • 嗯,将冒号放在正确的位置很有帮助...这正是需要做的。谢谢@thorsten-muller!
    • 别忘了身份证!!!! pets_attributes: [:id, :name, :category]否则,当您编辑时,每个宠物都会重新创建。
    • 你需要做Person.create(person_params) 否则它不会调用该方法。相反,你会得到ActiveModel::ForbiddenAttributesError
    • 另外,如果你想从表单中销毁项目,你还需要将隐藏的:_destroy参数列入白名单。即pets_attributes: [:id, :name, :category, :_destroy]
    【解决方案3】:

    今天我遇到了同样的问题,在使用 rails 4 时,我能够通过将 fields_for 结构化为:

    <%= f.select :tag_ids, Tag.all.collect {|t| [t.name, t.id]}, {}, :multiple => true %>
    

    然后在我的控制器中,我的强大参数为:

    private
    def post_params
        params.require(:post).permit(:id, :title, :content, :publish, tag_ids: [])
    end
    

    一切正常!

    【讨论】:

    • 嗨,谢谢@KingsleyIjomah - 如果您想将孩子的特定属性列入白名单怎么办?
    【解决方案4】:

    实际上有一种方法可以将所有嵌套参数列入白名单。

    params.require(:widget).permit(:name, :description).tap do |whitelisted|
      whitelisted[:position] = params[:widget][:position]
      whitelisted[:properties] = params[:widget][:properties]
    end
    

    此方法优于其他解决方案。 允许深度嵌套参数。

    而其他解决方案如:

    params.require(:person).permit(:name, :age, pets_attributes: [:id, :name, :category])
    

    不要。


    来源:

    https://github.com/rails/rails/issues/9454#issuecomment-14167664

    【讨论】:

      【解决方案5】:

      或者你可以简单地使用

      def question_params
      
        params.require(:question).permit(team_ids: [])
      
      end
      

      【讨论】:

        【解决方案6】:

        来自文档

        To whitelist an entire hash of parameters, the permit! method can be used
        
        params.require(:log_entry).permit!
        

        嵌套属性采用哈希的形式。在我的应用程序中,我有一个 Question.rb 模型接受 Answer.rb 模型的嵌套属性(用户在其中为他创建的问题创建答案选择)。在 questions_controller 中,我这样做了

          def question_params
        
              params.require(:question).permit!
        
          end
        

        问题哈希中的所有内容都是允许的,包括嵌套的答案属性。如果嵌套属性是数组形式,这也适用。

        话虽如此,我想知道这种方法是否存在安全问题,因为它基本上允许散列内的任何内容,而无需具体说明它是什么,这似乎与强参数的目的背道而驰。

        【讨论】:

        • 太棒了,我无法明确允许范围参数,这可以节省我一些时间。
        • 是的,使用 .permit!通常被视为潜在的安全问题。如果用户是管理员,你只会真正想使用它,但即使那样我也会对它的使用保持警惕。
        • 我的嵌套属性也在一个数组中。 .permit! 是唯一的选择吗?即使允许所有模型的属性,我也无法让它工作,因为它会阻塞数组。
        猜你喜欢
        • 2017-06-03
        • 2016-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-22
        相关资源
        最近更新 更多