Search code examples
ruby-on-railsruby-on-rails-4strong-parameters

Unpermitted Parameter in spite of being included in strong params


I have a situation where I am getting the error

Unpermitted parameter: incorporation

however I have it listed in the strong params:

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],

There's a bit of a catch that might be contributing to the issue: The controller in question is actually the Incorporation controller. But, as you might notice, we are using it to create a parent model Company, which has_one :incorporation. I realize that this is a bit odd, but I have reasons for wanting my models to be structured this way AND for using the incorporations_controller for doing it.

Accordingly, I have my form structured in the following way:

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

Thanks in advance for any insight

Update: My new and create methods are as follows in 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

Update 2: In Case it helps, here are the parameters from the log:

"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"}  

I noticed that (unlike other nested attributes) incorporation does not have the _attributes line after it. Might that be of some significance?

Update3: I also seem to be creating an incorporation entry in the incorporations table with the proper ownership assigned. However no other fields are filled out.


Solution

  • You shouldn't have incorporation in your submitted params anyway - it should be incorporation_attributes (as you've already got in your strong params).

    --

    If you're using fields_for, you should expect [association]_attributes to be passed as a parameter from your form.

    Not having it means you've either not got accepts_nested_attributes_for in your parent model, or you have not built your child object:

    #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
    

    Update

    What a strange issue you have - you're passing names_attributes fine and yet incorporation doesn't work.

    The one thing I would say, after looking at your params, is that your incorporation is only passing "submit" => "0". I don't see what that is; anyway there are numerous issues with your form:

    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
    

    This will allow you to...

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

    When using fields_for, you only need to pass the parent object (in your case @company). Building incorporation will automatically populate fields_for without explicitly declaring it.