<%= 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]]
最後你的解釋是有道理的,我真的不應該有領域。非常感謝你 – katie