Rails 3.HABTM 表单选择下拉菜单
我有一张发票表格。这是一个简化版本:因此它具有行项目,您可以在其中选择产品名称的下拉菜单。
这运行良好:因此发票-line_item 关系是这样的:发票 has_many line_items 且 line_item 属于发票。 line_item 属于 item 并且 item has_many line_items。我的 items、line_items 和发票设置正确。
但现在我想对行项目加税。
因此,我创建了一个 line_items_taxes 表来创建 line_items 和税收之间的 HABTM 关系。但我无法在表单中正确设置它。我的表单看起来像这样...
|name|price|tax|
| v| | v|
| v| | v|
| v| | v|
[save invoice]
所以我需要一个“税”下拉选择菜单,当保存发票时,它会保存每个行项目的税。
我已经尝试过 http://snippets.dzone.com/posts/show/4369 提供的解决方案 和 Rails HABTM 问题 但我得到错误。
未定义的方法合并:名称:符号 <%= f.collection_select "line_item", "tax_ids", @taxes, :id, :name, {:name => 'line_item[tax_ids][]'} %>
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][]'} %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对 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.)
It should instead look something like this:
That's a start in the right direction, anyway.