alex
alex

Reputation: 1089

Rails 5: Many-to-Many Relationship Not Working

I am trying to create a many-to-many table relationship in a Rails 5 project.

One Shop can have many Categories, which are indexed in the Shop_Categories table.

I seem to be missing one key step though. When I submit my Shop form, I receive the soft error: "Unpermitted parameter: :shop_category"

I can see my shop_category parameters being successfully sent in the Rails server log, but the Shop_Categories table doesn't get updated at all.

What am I missing, and what can I change so that pressing save on the Shop view form updates the Shop_Categories table?

Here's my code. Shop Model

class Shop < ApplicationRecord
    belongs_to :type

    has_many :shop_categories
    has_many :categories, through: :shop_categories

    accepts_nested_attributes_for :shop_categories

    enum status: [:open, :closed, :opening_soon, :closing_soon]
end

Shop_Category Model

class ShopCategory < ApplicationRecord
    belongs_to :shop
    belongs_to :category
end

Category Model

class Category < ApplicationRecord
    has_many :subcategories, dependent: :destroy

    has_many :shop_categories
    has_many :shops, through: :shop_categories
end

Shop Controller (Excerpt)

def new
    @shop = Shop.new
    @shop.shop_categories.build
end

def create
    @shop = Shop.new(shop_params)

    @shop.open_date = params[:shop][:open_date]+"-01"
    @shop.close_date = params[:shop][:close_date]+"-01"

    if @shop.save
        redirect_to @shop
    else
        render 'new'
    end
end

def update
    @shop = Shop.find(params[:id])

    if @shop.update(shop_params)
        redirect_to @shop
    else
        render 'edit'
    end
end

private
    def shop_params

        params[:shop][:open_date] = params[:shop][:open_date]+"-01"
        params[:shop][:close_date] = params[:shop][:close_date]+"-01"

        params.require(:shop).permit(:shop_name, :type_id, :status,
          :phone_number, :mobile_number, :email_address, :open_date,
          :close_date, shop_categories_attributes: [ :shop_id, :category_id] )
    end

Shop Form View (Excerpt)

<p>
    <%= form.label :shop_name %><br>
    <%= form.text_field :shop_name %>
</p>

<%= form.fields_for :shop_category do |category_fields| %>
    <p>
        <%= category_fields.label :category %><br />
        <%= category_fields.collection_select(:category_id, Category.all, :id, :category_name, include_blank: true) %>
    </p> 
<% end %>

<p>
    <%= form.submit %>
</p>

And finally, the Database Schema

create_table "categories", force: :cascade do |t|
    t.string "category_name"
    t.text "category_description"
    t.string "visible_category"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
end

create_table "shop_categories", force: :cascade do |t|
    t.string "visible_category_override"
    t.integer "shop_id"
    t.integer "category_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["category_id"], name: "index_shop_categories_on_category_id"
    t.index ["shop_id"], name: "index_shop_categories_on_shop_id"
end

create_table "shops", force: :cascade do |t|
    t.string "shop_name"
    t.integer "status"
    t.integer "sale"
    t.string "phone_number"
    t.string "mobile_number"
    t.string "email_address"
    t.date "open_date"
    t.date "close_date"
    t.integer "type_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["type_id"], name: "index_shops_on_type_id"
end  

Any suggestions would be greatly appreciated. I just can't work out which piece of the puzzle I'm missing!

Update: Including the Dev log, and the full Shop Form.

Dev Log

Started PATCH "/shops/1" for 127.0.0.1 at 2018-01-14 11:08:39 +0000
Processing by ShopsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"2qydDj9JovMrg9VT6Lo5xbcNSHl5MH0ylmq3IMMX4iRTAVSgK6JlXWpG+CyuwQK6svZJn9wtnEnZANUlOZYzXA==",
    "shop"=>{"shop_name"=>"test", "type_id"=>"1",
    "shop_categories"=>{"category_id"=>"1"}, "subcategory_id"=>"", 
    "status"=>"open", "phone_number"=>"123", "mobile_number"=>"13212", 
    "email_address"=>"[email protected]", "open_date"=>"2017-12",
    "close_date"=>"2017-12"}, "commit"=>"Update Shop", "id"=>"1"}
  Shop Load (0.5ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
Unpermitted parameters: :shop_categories, :subcategory_id
   (0.1ms)  begin transaction
  Type Load (0.2ms)  SELECT  "types".* FROM "types" WHERE "types"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.1ms)  commit transaction
Redirected to http://localhost:3000/shops/1
Completed 302 Found in 10ms (ActiveRecord: 0.9ms)

Full view/shops/_form.html.erb file

<%= form_with model: @shop, local: true do |form| %>

    <% if @shop.errors.any? %>
        <div id="error_explanation">
            <h2>
                <%= pluralize(@shop.errors.count, "error") %> prohibited
                this shop from being saved:
            </h2>
            <ul>
                <% @shop.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
    <% end %>

    <p>
        <%= form.label :shop_name %><br>
        <%= form.text_field :shop_name %>
    </p>

    <p>
        <%= form.label :type %><br />
        <%= form.collection_select(:type_id, Type.all, :id, :type_name) %>
    </p> 

    <%= form.fields_for :shop_categories do |category_fields| %>
        <p>
            <%= category_fields.label :category %><br />
            <%= category_fields.collection_select(:category_id, Category.all, :id, :category_name, include_blank: true) %>
        </p> 
    <% end %>

    <p>
        <%= form.label :subcategory %><br />
        <%= form.grouped_collection_select :subcategory_id, Category.all, :subcategories, :category_name, :id, :visible_subcategory, include_blank: true %>
    </p> 
    <p>
        <%= form.label :status %><br />
        <%= form.select :status, Shop.statuses.keys %> 
    </p>

    <p>
        <%= form.label :phone_number %><br>
        <%= form.telephone_field :phone_number %>
    </p>

    <p>
        <%= form.label :mobile_number %><br>
        <%= form.telephone_field :mobile_number %>
    </p>

    <p>
        <%= form.label :email_address %><br>
        <%= form.email_field :email_address %>
    </p>

    <p>
        <%= form.label :open_date %><br>
        <%= form.month_field :open_date %>
    </p>

    <p>
        <%= form.label :close_date %><br>
        <%= form.month_field :close_date %>
    </p>

    <p>
        <%= form.submit %>
    </p>

<% end %>

Upvotes: 0

Views: 283

Answers (1)

Tashows
Tashows

Reputation: 558

It should probably be :shop_categories, not :shop_category

<%= f.fields_for :shop_categories do |category_f| %>
  ...
<% end %>

You also kind of lost me in the Shop_Category Model part. The actual class name is different (ShopSubcategory) and it belongs_to a subcategory? This will probably cause some errors as well. I guess what you meant is the ShopCategoryjoin model and it belongs_to :category

Update

Also be sure to build your nested resource when creating a new form:

#shop_controller

def new
  @shop = Shop.new
  @shop.shop_categories.build
  ...
end

Upvotes: 1

Related Questions