Rails 3.HABTM 表单选择下拉菜单

发布于 2024-12-25 02:17:53 字数 876 浏览 3 评论 0原文

我有一张发票表格。这是一个简化版本:因此它具有行项目,您可以在其中选择产品名称的下拉菜单。

这运行良好:因此发票-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡の花香 2025-01-01 02:17:53

您对 collection_select 的调用包含一个额外的参数,该参数会导致问题发生。 (因为我假设您正在使用 *form_for*,所以会自动包含“line_item”参数,而您的参数是多余的。)

它应该看起来像这样:

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

无论如何,这是一个正确方向的开始。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文