Search code examples
ruby-on-railsformsrails-activerecordnested-attributesstrong-parameters

Assign parameters with strong parameters and nested models in rails 4


it's the first time that i make a question here. i tried many answers yet, but none have solved my problem.(Sorry for my english, i'm working to improve).

Okai, the issue is the following: I'm doing a e-shop and it have products. A product must have a category, but not necessarily a subcategory. The idea is that only creating a product, including the category and subcategory names as parameters, rails must create automatically the entries for the category and subcategory tables.

The problem is when i create a product with the form. When i do this, in the console i see: "Unpermitted parameters: category, subcategory"

This are the three models:

class Product < ActiveRecord::Base

    has_one :subcategory
    has_one :category, through: :subcategory

    accepts_nested_attributes_for :category, :subcategory
end

class Category < ActiveRecord::Base
    belongs_to :product
    has_many :subcategories

    accepts_nested_attributes_for :subcategories
end

class Subcategory < ActiveRecord::Base
    belongs_to :product
    belongs_to :category
end

The controller relevant part:

def create
    @titulo = "Catálogo Online"
    @product = Product.create(product_params)

    respond_to do |format|
        if @product.save
            format.html { redirect_to @product, notice: 'Producto creado.' }
        else
            format.html { render :new }
        end
    end
end

...

def product_params
  params.require(:product).permit(:name, :description, :price, :stock, :code, category_attributes: [:id, :category, :product_id ], subcategory_attributes: [:id, :subcategory, :category_id, :product_id ])
end

And the form

<%= form_for(@product) do |f| %>
  <% if @product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
      <% @product.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :category %><br>
    <%= f.text_field :category %>
  </div>
  <div class="field">
    <%= f.label :subcategory %><br>
    <%= f.text_field :subcategory %>
  </div>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
    <div class="field">
    <%= f.label :code %><br>
    <%= f.text_field :code %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.number_field :price %>
  </div>
  <div class="field">
    <%= f.label :stock %><br>
    <%= f.check_box :stock %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Please, I been three days with this problem. i don't know what to do. Thanks.


Solution

  • The problem, I would say, is that you are using a text_field input for category and sub_category. In order for this to work, you should use a select field and then provide another way in the UI to create categories and sub_categories.

    Replacing f.text_field :subcategory with (and removing the :category option because your data model will not allow this) something like...

    <%= f.collection_select :subcategory_id, Subcategory.all, :id, :name, { selected: @product.subcategory_id } %>
    

    …should enable you to assign a product to a subcategory.

    Also, your data model looks incorrect here. The product needs to belong to the subcategory, not the other way round.