嵌套属性未允许的参数

时间:2013-04-10 07:39:02

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

我有一个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

票据/ _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 %>

票据/ _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有没有变化?

6 个答案:

答案 0 :(得分:177)

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

这应该是这样的:

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

中找到更多详情。

答案 1 :(得分:20)

来自文档

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

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

话虽如此,我想知道这种方法是否存在安全问题,因为它基本上允许散列内部的任何内容而不确切地指明它是什么,这似乎与强参数的目的相反。

答案 2 :(得分:20)

或者你可以简单地使用

def question_params

  params.require(:question).permit(team_ids: [])

end

答案 3 :(得分:11)

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

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

答案 4 :(得分: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

一切正常!

答案 5 :(得分:1)

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

相关问题