未经允许的参数尽管包含在强参数中

Unpermitted Parameter in spite of being included in strong params

我遇到了错误

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 控制器。但是,您可能会注意到,我们正在使用它来创建父模型 Companyhas_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 %>

提前感谢您的任何见解

更新:我的newcreate方法如下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"=>{"14553672570"=>{"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"}  

我注意到(与其他嵌套属性不同)incorporation 后面没有 _attributes 行。这可能有点意义吗?

Update3: 我似乎也在公司 table 中创建一个公司条目,并分配了适当的所有权。但是没有填写其他字段。

错误表明我们需要在company模型中定义这个:

accepts_nested_attributes_for :incorporation
attr_accessible :incorporation_attributes

你不应该在你提交的参数中有 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 而无需明确声明。