【问题标题】:Unpermitted Parameter in spite of being included in strong params尽管包含在强参数中,但未经允许的参数
【发布时间】:2016-01-15 08:46:21
【问题描述】:

我遇到错误的情况

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 %>

提前感谢您的任何见解

更新:我的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 行。这可能有什么意义吗?

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

【问题讨论】:

  • incorporation 未列在您的强参数中。再试一次。

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


【解决方案1】:

无论如何,您不应该在提交的参数中包含 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" =&gt; "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.rb 中同时拥有has_one :incorporationaccepts_nested_attributes_for :incorporation。我的newcreate 方法已添加到上述问题中。我相信它们相当于你在这里拥有的东西,但我可能是错的。再次感谢。
  • 谢谢,让我帮你看看
  • 我还查看了日志中的参数,注意到incorporation 没有遵循标准_attributes。 (请参阅上面的其他更新)这可能是...的线索吗?
  • 好吧,我确实在答案的开头解释了这一点:)
  • 您对视图的修改成功了。实际上,我以前曾因类似的问题而受到指责。非常感谢您的帮助!
【解决方案2】:

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

accepts_nested_attributes_for :incorporation
attr_accessible :incorporation_attributes

【讨论】:

  • 标签ruby-on-rails-4strong-parameters 表示使用了强参数,而attr_accessible 是..好吧,不使用它:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多