RoR:collection_select 未在数据库中设置值

RoR:collection_select 未在数据库中设置值

问题描述:

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

I am using Rails 3 and I am having a problem. My collection_select is not populating the field in the DB. This is what my view looks like: (some html was removed)

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 的表.我的语法正确吗?

The drop down list has the correct values, but when I submit it, this is what my params looks like: "Company"=>{"company_id"=>"2"}. I don't have a field called company, but I do have a field called company_id and a table called company. Is my syntax correct?

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

试试这个

# 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