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

时间:2016-01-15 08:46:21

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

我遇到错误的情况

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"=>{"145\2853672570"=>{"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行。可能有一些意义吗?

Update3:我似乎也在公司合并表中创建了一个合并条目,并指定了正确的所有权。但是没有填写其他字段。

2 个答案:

答案 0 :(得分:2)

你提交的参数中不应该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,而不会明确声明它。

答案 1 :(得分:0)

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

accepts_nested_attributes_for :incorporation
attr_accessible :incorporation_attributes