2013-10-25 85 views
0

後空永諾我有兩個型號,Rails的外鍵是爲節省

class ProcessType < ActiveRecord::Base 
has_many :remarks 

validates :code, :name, presence: true 
end 

class Remark < ActiveRecord::Base 
belongs_to :process_type 
belongs_to :origin 

validates :description, presence: true 
end 

外鍵的設置是否正確,在言論表有process_type_id列。

在創建形式,我展示的過程可以使用select:

<%= simple_form_for @remark do |f| %> 
    <%= collection_select(:remark, :process_type_id, ProcessType.all, :id, :name) %> 

    <%= f.input :description, label: 'Description' %> 
    <%= f.input :suggestion, label: 'Suggestion' %> 

    <%= f.button :submit %> 
<% end %> 

我的問題是,在備註表,保存後,該進程的ID總是空。我錯過了什麼?可能是顯而易見的,但我現在無法看到它。

謝謝!

回答

0

試試這個:

<%= f.collection_select :process_type_id, ProcessType.all, :id, :name %> 

,或者,如果你想使用的簡單表格,方法:

<%= f.input :process_type_id, :collection => ProcessType.all %> 
+0

嘗試都只是現在,同樣的結果,空數據庫。 –

+0

你使用Rails 4嗎?你是否在'remark_params'中允許這個字段? – depa

+0

是的,鐵軌4.但我不知道你對remark_params說什麼。 –