Rails 3. HABTM form select drop down menu

 

Questions


I have an invoice form. This is a simplified version: so it has line items where you select a drop down menu of product names.

This is working well: So the invoice-line_item relations is this: invoice has_many line_items and line_item belongs to invoice. line_item belongs to item and item has_many line_items. I have the items, line_items and invoice setup correctly.

But now I want to add taxes to the line items.

So I created a line_items_taxes table to create a HABTM relationship between line_items and taxes. But I can’t set it up correctly in the form. My form looks like this…

|name|price|tax|
|   v|     |  v|
|   v|     |  v|
|   v|     |  v|
[save invoice]

So I need a TAXES drop down select menu, and when the invoice is saved, it saves the tax for each line item.

I have tried the solutions offered at http://snippets.dzone.com/posts/show/4369 and Rails HABTM Question but I get errors.

undefined method merge for :name:Symbol
<%= f.collection_select "line_item", "tax_ids", @taxes, :id, :name, {:name => 'line_item[tax_ids][]'} %>

 

 

————————————————-

Answer

Your call to collection_select contains an extra parameter that is throwing things off. (Since I assume you are using *form_for*, the ‘line_item’ argument is automatically included, and yours is redundant.)

It should instead look something like this:

f.collection_select 'tax_ids', @taxes, :id, :name, {:name => 'line_item[tax_ids][]'}

That’s a start in the right direction, anyway.

associations,forms,has-and-belongs-to-many,ruby,ruby-on-rails

Facebook Comments

Post a comment