RoR:collection_select未在DB中设置值

时间:2011-05-17 05:10:40

标签: ruby-on-rails-3

我正在使用Rails 3,我遇到了问题。我的collection_select未填充数据库中的字段。这就是我的观点:(某些html被删除)

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

1 个答案:

答案 0 :(得分: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