2011-10-07 75 views
0
<%= collection_select(:category, :category_id, Category.all, :id, :category_name ,:prompt=>"category") %> 

不會插入在參數,你可以看到CATEGORY_ID有table.Even雖然交易CATEGORY_ID,但它不會傳遞到交易數據庫table.Here是的休息code.Any幫助將appreciated.Thank你collection_select犯規提交value_id到數據庫

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

型號

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

    class Category < ActiveRecord::Base 
has_many :deals 
    end 

在日誌

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

回答

2

嘗試使用:

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

沒有

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

因爲這筆交易所屬的類別,你將在交易模型中的CATEGORY_ID或CATEGORY_ID場不需要

accepts_nested_attributes_for :category 

除非您嘗試在交易的同一時間創建新類別。

+0

最後你的解釋是有道理的,我真的不應該有領域。非常感謝你 – katie

1

看看參數。 category_id在交易散列之外(而item_name和stores_attributes在裏面)。

你可能想<%= c.collection_select ... %>

擺脫周圍的類別fields_for的選擇 - 沒有理由。 category_id是交易的屬性,而不是類別。

+0

<%= d.collection_select ...%>給我的錯誤爲#某種原因未定義的方法'CATEGORY_ID」 <類別:0xa8a49b8> – katie

+0

因爲CATEGORY_ID不是屬性的類別。爲什麼你在類別選擇周圍使用fields_? <%= c.collection_select:category,...%>應該可以正常工作。 –

+0

我正在使用field_for,因爲我在交易模型中有:accep_nested_attributes_for:stores,:category,對不起,我沒有顯示它。我已經在上面更新過了。即使移除仍然沒有幫助 – katie

0

最後這一項工作

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