Rails的3 HABTM形式选择下拉菜单 [英] Rails 3. HABTM form select drop down menu
问题描述
我有发票的形式。这是一个简化版本:所以它在其中选择一个下拉产品名称的菜单行项目。
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.
这是正常工作:所以发票LINE_ITEM关系是这样的:发票的has_many line_items和LINE_ITEM属于发票。 LINE_ITEM属于项目,项目的has_many line_items。我有项目,line_items和发票设置正确。
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.
所以,我创建了一个line_items_taxes表中创建line_items和税收之间的关系HABTM。但我不能在正确的形式对其进行设置。我的表看起来像这样...
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.
我曾尝试在 http://snippets.dzone.com/posts/show提供的解决方案/ 4369 和 Rails的HABTM问题,但我得到的错误。
I have tried the solutions offered at http://snippets.dzone.com/posts/show/4369 and Rails HABTM Question but I get errors.
未定义的方法合并为:名称:符号
<%= f.collection_selectLINE_ITEM,tax_ids,@taxes,:ID,:姓名,{:名称=> LINE_ITEM [tax_ids] []'}%GT;
undefined method merge for :name:Symbol
<%= f.collection_select "line_item", "tax_ids", @taxes, :id, :name, {:name => 'line_item[tax_ids][]'} %>
推荐答案
您到collection_select调用包含被扔东西了一个额外的参数。 (因为我假设你使用的是* *的form_for的'LINE_ITEM'参数自动包括在内,你是多余的。)
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.)
应该不是这个样子:
f.collection_select 'tax_ids', @taxes, :id, :name, {:name => 'line_item[tax_ids][]'}
这是朝着正确方向的开始,反正。
That's a start in the right direction, anyway.
这篇关于Rails的3 HABTM形式选择下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!