2012-12-03 27 views
0

我有两个型号:问题在数据库中使用collection_select储存价值

Project.rb

class Project < ActiveRecord::Base 
belongs_to :customer 
end 

和Customer.rb

class Customer < ActiveRecord::Base 
    has_many :projects 
end 

里面的_form.html.erb我有:

<p> 
    <label>Select Customer</label> 
    <%= f.collection_select :customer_id, Customer.all, :id, :name, :include_blank => true %> 
</p> 

Whic h应该从客户模型中收集客户并显示所有客户,最后应将值分配给项目表中的customer_id。

现在,当我检查日志时,一切都在传递。当我选择value = 1的第一个客户时,它会在我的日志中传递customer_id =“1”,但它不会存储在表中。它在项目表中显示customer_id = nil。

有人可以帮忙。谢谢:)

回答

3

做检查,你在attr_accessible方法一样加入CUSTOMER_ID,

class Project 
    attr_accessible :your_other_attributes, :customer_id 
end 
+0

你是男人:)谢谢你这么多桑托斯 – Supersonic