Ruby on rails 3 - RoR:collection_select不在数据库中设置值



我正在使用 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

试试这个

# 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

相关内容

  • 没有找到相关文章

最新更新