2013-01-20 35 views
0

我有一個表單,用戶添加一個新項目,並且作爲其中的一部分,他們爲項目選擇一個類別。該項目可以是存款或賬單,所以我想要兩個不同的列表,他們可以從類別中選擇,因爲它們以相同的方式分割。這是我的主題和itemcategory模型與我的表單發生衝突與相關模型

class Ledgeritem < ActiveRecord::Base 
    attr_accessible :amount, :bankaccount_id, :deposit, :itemcategory_id, :name, :processed, :transactiondate 

    attr_accessor   :balance 
    attr_accessible   :balance 

    belongs_to :bankaccount 
    belongs_to :itemcategory 

end 

class Itemcategory < ActiveRecord::Base 
    attr_accessible :deposit, :itemcategory_id, :name, :user_id 

    has_many :ledgeritems 
end 

我有我的控制器。我最初是在我的模型中使用它,但將它移到控制器上,看看是否是這個問題。

@bill_categories = Itemcategory.all.where("deposit = 0") 
@deposit_categories = Itemcategory.all.where("deposit = 1") 

這是我如何使用它的形式

<%= f.label :itemcategory_id %><br /> 
<%= f.select :itemcategory_id, @bill_categories %> 

,這裏是我得到

can't convert Symbol into Integer 

錯誤我敢肯定它必須做與我的f.select格式,我只是無法弄清楚什麼。

回答

1

您需要提供f.select的選項。您可以使用options_from_collection_for_select爲:

<%= f.select :itemcategory_id, options_from_collection_for_select(@bill_categories, 'id', 'name') %> 

這將提供有value設置爲對象的id@bill_categories和顯示將成爲對象的name財產@bill_categories文本選項。

+0

啊,我知道這件事很簡單。 – Jhorra

+0

仍然收到相同的錯誤。 – Jhorra

+0

你確定它在那條線上嗎?我不確定其他地方是否存在其他問題(可能在form_for的定義中),但這個options_from_collection_for_select解決方案將在未來將您從錯誤中拯救出來。 :) –