2011-05-17 77 views
0

我正在使用Rails 3,並遇到問題。我的collection_select未填充數據庫中的字段。這就是我的看法是這樣的:(除去一些HTML)RoR:collection_select未設置數據庫中的值

f.label "Company Name" 
@c = Company.all 
collection_select(:Company , :company_id, @c, :id , :companyName, {:prompt => true}) 
f.submit 

下拉列表中有正確的價值觀,但是當我提出它,這是我的params樣子:"Company"=>{"company_id"=>"2"}。我沒有一個名爲company的字段,但我確實有一個名爲company_id的字段和一個名爲company的表。我的語法正確嗎?

Processing by RecordsController#create as HTML 

Parameters: {"utf8"=>"âœ「", "authenticity_token"=>"WLzwCoZFCUhyH9ZqYWYLmaQwyleB3JLoTnb4lhftKzc=", "record"=>{"title"=>"test", "Company"=>{"company_id"=>"2"}, "commit"=>"Create record"} 

AREL (2.0ms) INSERT INTO "records" ("company_id", "title", "created_at", "updated_at") VALUES (NULL, 'test','2011-05-17 05:01:38.905717', '2011-05-17 05:01:38.905717') 
Redirected to http://localhost:3000/Records/7 
Completed 302 Found in 150ms 
+0

不知道這會有所幫助,但我想清楚了一點件事了。在Rails中,您將擁有一個頂級參數,它保存相關模型對象的參數。因此,指向實際參數的「公司」是要發送到update_attributes的東西,或者是新的或您調用的任何影響數據庫中模型的東西。 – jaydel 2011-05-17 17:57:29

回答

1

試試這個

# in controller 
@companies = Company.all 

collection_select @record, :company_id, @companies, :id, :companyName 

form_for @record do |f| 
    = f.collection_select :company_id, @companies, :id, :companyName 
+0

這解決了我的問題。謝謝 – Jason 2011-05-22 00:59:43