Rails 3. HABTM form select drop down menu

空扰寡人 提交于 2019-12-10 21:16:46

问题


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][]'} %>


回答1:


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.



来源:https://stackoverflow.com/questions/8735242/rails-3-habtm-form-select-drop-down-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!