0

When i create or edit product, my merchant_product_detail will not be created or edit due to
Unpermitted parameter: merchant_id. But other like id and price are able to pass strong parameters, only merchant_id could not pass it. Kindly help me why my merchant_id is not permit in this case?

params return in binding.pry mode

"product"=>
      {"name"=>"fewrgvers",
       "description"=>"",
       "product_categories_attributes"=>{"0"=>{"id"=>""}},
       "merchant_product_details_attributes"=>
        {"0"=>{"merchant_id"=>["2"], "id"=>"", "price"=>"123"}}
    "

product_params return

    Unpermitted parameter: merchant_id
    => {"name"=>"fewrgvers",
     "description"=>"",
     "merchant_product_details_attributes"=>{"0"=>{"id"=>"", "price"=>""}}

product.rb

has_many :merchant_product_details
accepts_nested_attributes_for :merchant_product_details, reject_if: proc { |attributes| attributes['merchant_id'].blank? }
has_many :merchants, through: :merchant_product_details

merchant.rb

has_many :merchant_product_details
has_many :products, through: :merchant_product_details
accepts_nested_attributes_for :merchant_product_details

merchant_product_detail.rb

belongs_to :product
belongs_to :merchant

product_controller.rb

 def new
    @product = Product.new
    @product.merchant_product_details.build
end

def create
    @product = Product.new(product_params)
    respond_to do |format|
      if @product.save
        format.html { redirect_to root_path, notice: 'Product was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end
end

def update
    respond_to do |format|
      if @product.update_attributes(product_params)
        format.html { redirect_to root_path, notice: 'Product was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
end


params.require(:product).permit(:name, :description, 
      merchant_product_details_attributes: [:id, :merchant_id, :price]

_form.html.erb

<%= form_for(@product, :url => path, html: { class: "form-horizontal", role: "form" }) do |f| %>
<%= f.fields_for :merchant_product_details do |builder| %>
    <div class="form-group row">
      <%= builder.label :merchant_id, class: "col-md-2 control-label" %>
      <div class="col-md-8">
        <%= builder.select :merchant_id, Merchant.all.collect {|x| [x.name, x.id]}, {include_hidden: false} ,prompt: "Select something", multiple: true, class: "select2" %>
        <%= builder.hidden_field :id %><br>
              <%= builder.label :price %>
        <%= builder.number_field :price %>
      </div>
<% end %>

1 Answer 1

1

The problem is the multiple: true in the form field for merchant_id. This means that the param will be an array, as it could be multiple merchant ids.

If this is what you want then I would recommend changing the name to merchant_ids and allow an array like this:

params.require(:product).permit(:name, :description, 
  merchant_product_details_attributes: [:id, :price, merchant_ids: []])

Having a look at your model relations I think you only want to have one id though, in which case it should be enough to remove the multiple: true in the select.

<%= builder.select :merchant_id, Merchant.all.collect {|x| [x.name, x.id]}, {include_hidden: false}, prompt: "Select something", class: "select2" %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.