错误:具有嵌套属性的未允许参数

时间:2014-12-16 05:56:39

标签: ruby-on-rails ruby-on-rails-4 nested-forms nested-attributes strong-parameters

我在保存嵌套字段的内容时遇到问题。我有两个模型IncorporationCompany。它们涉及如下:

class Company < ActiveRecord::Base
  belongs_to :incorporation
end

class Incorporation < ActiveRecord::Base
  has_one :company

  accepts_nested_attributes_for :company
end

我的目标是使用合并控制器和视图以相同的形式创建新的CompanyIncorporation条目。

问题)但是,每次我尝试提交表单时,公司条目都会通过,但公司条目会因Unpermitted parameters错误而停止:

Started POST "/incorporations" for 127.0.0.1 at 2014-12-15 22:40:59 -0700
Processing by IncorporationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"lCj/ZtNNE/9l/UAlYcnA8EAe8vmMN010toS4t5e+ZkA=", "incorporation"=>{"title"=>"test", "company"=>{"name"=>"test"}}, "button"=>""}
Unpermitted parameters: company
Completed 500 Internal Server Error in 4ms

这是特别令人惊讶的,因为我相信我已经正确地设置了强大的障碍。下面是我的控制器。

class IncorporationsController < ApplicationController
  def index
  end

  def show
  end

  def new
@incorporation = Incorporation.new
@company = Company.new
  end

  def create
@incorporation = Incorporation.new(incorporation_params)

if @incorporation.save
  redirect_to @incorporation, notice: "Successfuly saved incorporation info."
else
  render 'new'
end
  end

  def edit
  end

  def show
  end

  private

  def incorporation_params
params.require(:incorporation).permit(:title, company_attributes: [:name, :state_corp, :street, :city, :state, :zip, :outstanding_common_stock, :fiscal_year_end_month, :fiscal_year_end_day])
  end
end

我正在使用的表格部分如下:

<%= form_for @incorporation do |f| %>
  <div class="panel-body">
    <%= f.text_field :title, input_html: { class: 'form-control' } %>
    <h3>TEST</h3>
    <%= f.fields_for @company do |company| %>
      <%= company.text_field :name, input_html: { class: 'form-control' } %>
    <% end =%>
  </div>
  <%= f.button :submit, class: "btn btn-primary" %>

<% end =%>

任何想法都会受到赞赏。

2 个答案:

答案 0 :(得分:1)

执行上述操作的常用方法是

控制器

  def new
  @incorporation = Incorporation.new
  @company = @incorporation.build_company

并在您的视图中

<%= form_for @incorporation do |f| %>
  <div class="panel-body">
    <%= f.text_field :title, input_html: { class: 'form-control' } %>
    <h3>TEST</h3>
    <%= f.fields_for :company do |company| %>
      <%= company.text_field :name, input_html: { class: 'form-control' } %>
    <% end =%>
  </div>
  <%= f.button :submit, class: "btn btn-primary" %>
<% end %>

其余的都很好。

答案 1 :(得分:0)

交叉检查代码库中的一些内容。

1)您应该在公司表中包含incorporation_id。如果你没有,那就添加一个。

2)尝试在新操作中更改以下代码:

def new
  @incorporation = Incorporation.new
  @incorporation.company = Company.new
  #Or you can simply build company also like below
  #@incorporation.company.build
end

在视图中尝试更改:

<%= form_for @incorporation do |f| %>
  <div class="panel-body">
    <%= f.text_field :title, input_html: { class: 'form-control' } %>
    <h3>TEST</h3>
    <%= f.fields_for :company do |company| %>
      <%= company.text_field :name, input_html: { class: 'form-control' } %>
    <% end =%>
  </div>
  <%= f.button :submit, class: "btn btn-primary" %>

<% end =%>