1
我目前有點卡住找出一個優雅的解決我下面 問題:在控制器中處理collection_select的優雅方式?
比方說,我有以下類別:
class Event < ActiveRecord::Base
belongs_to :reg_template, :class_name => "EmailTemplate"
[...]
end
class EmailTemplate < ActiveRecord::Base
has_many :events
[...]
end
和包含一個觀點:
<%= f.collection_select(:reg_template_id, EmailTemplate.all, :id, :name) %>
在動作 控制器中處理此表單字段的建議方式是什麼?
有一個1:事件和的emailTemplate之間1關係意味着導軌 不產生reg_template_id和reg_template_id =方法(因爲這將 對於1做:N的關係),所以嘗試讀取或分配該字段將失敗 與
unknown attribute: reg_template_id
試圖調用
Event.update_attributes
使用時
<%= f.collection_select(:reg_template, EmailTemplate.all, :id, :name) %>
代替也沒有太大的幫助與它將會失敗:
EmailTemplate(#70070455907700) expected, got String(#70070510199800)
我想我一定是失去了一些東西,因爲我認爲這是非常明顯的,而 常見的有一個參考來更新模型實例另一個對象通過 collection_select。
事件表的外鍵名爲'reg_template_id',但不幸的是用它產生了'未知屬性:reg_template_id'錯誤我上面提到過。將其名稱改爲'email_template_id'會改變這種情況嗎? – 2010-03-02 08:15:57
外鍵「reg_template_id」應該工作。當你創建一個Event對象的實例並訪問'reg_template_id'時會發生什麼?即'Event.new.reg_template_id' – 2010-03-02 09:09:27
您是否運行過所有遷移? – nanda 2010-03-02 12:14:34