Jhorra
Jhorra

Reputation: 6321

Running into a conflict with in my form with related models

I have a form where a user adds a new item, and as part of this they choose a category for the item. The item can be a deposit or a bill, so I want two different lists they can choose from for categories since they are split up the same way. Here is my ledgeritem and itemcategory models

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

I have this in my controller. I originally had it in my model, but moved it to the controller to see if that was the problem.

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

This is how I'm using it in the form

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

And here is the error I'm getting

can't convert Symbol into Integer

I'm pretty sure it has to do with the format of my f.select, I just can't figure out what.

Upvotes: 0

Views: 42

Answers (1)

Marc Baumbach
Marc Baumbach

Reputation: 10473

You need to provide options to the f.select. You can use options_from_collection_for_select for that:

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

That will provide options that have a value set to the id of the objects in @bill_categories and the text that is displayed will be the name property of the objects in @bill_categories.

Upvotes: 1

Related Questions