katie
katie

Reputation: 2951

collection_select doesnt submit value_id to the database

<%= collection_select(:category, :category_id, Category.all, :id, :category_name ,:prompt=>"category") %>

wont insert category_id in the deals table.Even though in the parameters you can see category_id is there,but it is not passed to the deal database table.Here is the rest of the code.Any help will be appreciated.Thank you

   <%= form_for @deal ,:url=>{:action =>"create"} do |c|%>

    <%= c.text_field :item_name %><br/>
    <%=c.fields_for :stores do |s| %>
    <%=s.text_field :store_name %>
   <%end%>
   <%=c.fields_for :category  do |d| %>
   <%= collection_select(:category, :category_id, Category.all, :id, :category_name ,:prompt=>"category") %>

  <%end%>
  <%= c.submit "post"%>

   <%end%>

Model

    class Deal < ActiveRecord::Base
belongs_to :category
    accepts_nested_attributes_for :category 
    end

    class Category < ActiveRecord::Base
has_many :deals
    end

In the log

    Parameters: {"utf8"=>"✓",    "authenticity_token"=>"oO5AtFX4HUYAhcP15y/dFzn3kjVDmweykQPqgDDuupQ=", "deal"=> {"item_name"=>"grapes", "stores_attributes"=>{"0"=>{"store_name"=>"winco"}}},  "category_id"=>"2", "commit"=>"post"}
    City Load (0.1ms)  SELECT "cities".* FROM "cities" WHERE "cities"."id" = ? LIMIT  1  [["id", 2]]
    SQL (1.5ms)  INSERT INTO "deals" ("brand", "category_id", "city_id",  "created_at", "item_name", "price", "size", "stars", "updated_at") VALUES (?, ?, ?, ?, ?,  ?, ?, ?, ?)  [["brand", nil], ["category_id", nil], ["city_id", 2], ["created_at", Fri, 07  Oct 2011 23:16:45 UTC +00:00], ["item_name", "grapes"], ["price", nil], ["size", nil],  ["stars", nil], ["updated_at", Fri, 07 Oct 2011 23:16:45 UTC +00:00]]
     SQL (0.5ms)  INSERT INTO "stores" ("address", "created_at", "store_name",  "updated_at") VALUES (?, ?, ?, ?)  [["address", nil], ["created_at", Fri, 07 Oct 2011  23:16:45 UTC +00:00], ["store_name", "winco"], ["updated_at", Fri, 07 Oct 2011 23:16:45 UTC +00:00]]
    SQL (0.9ms)  INSERT INTO "store_deals" ("address", "created_at", "deal_id",  "store_id", "store_name", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["address", nil],   ["created_at", Fri, 07 Oct 2011 23:16:45 UTC +00:00], ["deal_id", 4], ["store_id", 4], ["store_name", nil], ["updated_at", Fri, 07 Oct 2011 23:16:45 UTC +00:00]]

Upvotes: 0

Views: 314

Answers (3)

katie
katie

Reputation: 2951

Finally this one worked

<%= c.collection_select( :category_id, Category.all ,:id,:category_name,:prompt=>"category") %>

Upvotes: 0

Mihai
Mihai

Reputation: 1262

Try using :

<%= c.collection_select(:category_id, Category.all, :id, :name ,:prompt=>"category")%>
or
<%= c.collection_select(:Category_id, Category.all, :id, :name ,:prompt=>"category")%>
or
<%= c.select :category_id, Category.all.collect { |v| [ v.name, v.id ] } %>

without

        <%=c.fields_for :category  do |d| %>

Because the Deal belongs to the Category you will have the category_id or Category_id field in the Deal model you don't need

accepts_nested_attributes_for :category 

unless you are trying to create a new category in the same time with the deal.

Upvotes: 2

Joe Van Dyk
Joe Van Dyk

Reputation: 6950

Look at the params. category_id is outside of the deal hash (whereas item_name and stores_attributes are inside).

You probably want <%= c.collection_select ... %>

Get rid of the fields_for around the category select -- there is no reason for it. category_id is an attribute on deals, not category.

Upvotes: 1

Related Questions