>我遇到错误的情况
Unpermitted parameter: incorporation
但是我将其列在强参数中:
def company_params
params.require(:company).permit(:id, :name, :employee_stock_options, :options_pool, :state_corp, :street, :city, :state, :zip, :_destroy,
incorporation_attributes: [:title, :trademark_search, :user_id, :employee_stock_options, :final_submit, :submit, :_destroy],
names_attributes: [:id, :name_string, :suffix, :approved, :snapshot, :company_id, :_destroy],
有一个问题可能会导致这个问题:有问题的控制器实际上是Incorporation
控制器。 但是,正如您可能注意到的那样,我们正在使用它来创建父模型Company
,它has_one :incorporation
。 我意识到这有点奇怪,但我有理由希望我的模型以这种方式构建并使用incorporations_controller
来做到这一点。
因此,我的表格结构如下:
<%= simple_form_for @company, url: url_for(action: @caction, controller: 'incorporations'), html: {id:"incorporationform"}, remote: false, update: { success: "response", failure: "error"} do |company| %>
<%= company.simple_fields_for @incorporation do |f| %>
<div class="padded-fields">
<div class="form_subsection">
<%= f.input :trademark_search, as: :radio_buttons, label: 'Would you like us to do a trademark search and provide advice regarding any issues we identify in relation to the name you have selected?', input_html: { class: 'form-control radio radio-false' } %>
</div>
</div>
<% end %>
....
<% end %>
提前感谢您的任何见解
更新:我的new
和create
方法如下incorporations_controller
def new
@user=current_user
@company = @user.companies.build
@incorporation = @company.build_incorporation
@action = "new"
@caction = "create"
end
def create
@snapshot="incorporation"
@company = current_user.companies.build(company_params)
@incorporation = @company.build_incorporation
if @company.save
current_user.companies << @company
if params[:final_submit]
redirect_to incorporations_index_path
else
redirect_to edit_incorporation_path(@incorporation), notice: "Successfuly saved incorporation info."
end
else
render 'new', notice: "Something went wrong; form unable to be saved."
# render :nothing => true
end
end
更新2:如果有帮助,以下是日志中的参数:
"company"=>{"names_attributes"=>{"1452853672570"=>{"name_string"=>"test19", "suffix"=>"INC", "_destroy"=>"false"}}, "fiscal_year_end_month"=>"", "fiscal_year_end_day"=>"", "street"=>"", "city"=>"", "state"=>"", "zip"=>"", "issued_common_stock"=>"10,000,000", "employee_stock_options"=>"false", "options_pool"=>"0", "incorporation"=>{"submit"=>"0"}}, "commit"=>"Save"}
我注意到(与其他嵌套属性不同)合并后面没有_attributes
行。 这是否具有某种意义?
更新3:我似乎还在公司合并表中创建了一个公司条目,并分配了适当的所有权。 但是,没有填写其他字段。
您不应该在提交的参数中incorporation
- 它应该是incorporation_attributes
的(因为您已经在强参数中得到了)。
--
如果您使用的是 fields_for
,您应该期望[association]_attributes
作为参数从表单传递。
没有它意味着您的父模型中没有accepts_nested_attributes_for
,或者您没有构建您的子对象:
#app/models/company.rb
class Company < ActiveRecord::Base
has_one :incorporation
accepts_nested_attributes_for :incorporation
end
--
#app/controllers/incorporations_controller.rb
class IncorporationsController < ApplicationController
def new
@company = Company.new
@company.build_incorporation #-> only needed if a new record
end
def create
@company = Company.new company_params
@company.save
end
end
更新
你有一个多么奇怪的问题 - 你通过names_attributes
很好,但incorporation
不起作用。
了你的params
后,我想说的一件事是,你的incorporation
只是路过"submit" => "0"
。我不明白那是什么;无论如何,您的表单存在许多问题:
def new
@company = current_user.companies.new
@company.build_incorporation
...
end
def create
@company = current_user.companies.new company_params
@company.save #-> don't need to "build" in create
end
这将允许您...
<%= simple_form_for @company, url: url_for(action: @caction, controller: 'incorporations'), html: {id:"incorporationform"}, remote: false, update: { success: "response", failure: "error"} do |company| %>
<%= company.simple_fields_for :incorporation do |f| %>
<%= f.input ...
<% end %>
使用 fields_for
时,您只需要传递父对象(在您的情况下为 @company
)。生成incorporation
将自动填充fields_for
,而无需显式声明它。
该错误表明我们需要company
模型中定义它:
accepts_nested_attributes_for :incorporation
attr_accessible :incorporation_attributes