RoR 3 创建发票应用程序 - 如何创建 HABTM 发票/产品关联的表单?

发布于 2024-12-10 20:20:48 字数 1463 浏览 0 评论 0原文

我正在尝试申请发票。以下是与我的问题相关的模型:

更新:由于最近的建议,模型信息已更改

Invoice  
> id  
> created_at  
> sales_person_id

LineItem
> id  
> invoice_id  
> item_id  
> qty_commit (inventory only)  
> qty_sold  
> price (because prices change)  
> ...etc

Item
> barcode  
> name  
> price
> ...etc

发票 has_many items, :through =>; :line_items。项目同上。我想要做的是,当我创建新发票时,我希望表单中填充所有可用的项目。我唯一不想填充所有项目的情况是在查看发票时(因此只应检索 LineItems 表中存在的项目)。目前 - 显然 - 新发票没有项目。当集合中当前没有任何内容时,如何列出它们,以及如何填充表单?另外,我希望所有产品在创建失败时都可用(以及用户通过表单选择的产品)。

更新:我可以通过以下方式通过控制器创建项目:

@invoice = Invoice.new
# 使用所有产品填充发票,以便可以选择它们
Item.where("库存 > ?", 0).each do |i|
@invoice.items.new(i.attributes)
结束

当然是我做我想做的事情的粗略尝试。从视觉上看,效果很好,但正如预测的那样,当我实际尝试保存模型时,我的表单 ID 等并没有很好地发挥作用。

预期为 LineItem(#37338684),得到数组(#2250012)

形式的示例:

# f 是 form_for
<% @invoice.items.group_by{|p| p.category}.each |类别,产品| %>

<%= 类别.名称 %>

<%= f.fields_for :line_items 执行 |line_item| %>
<% for p in products %>

<%= line_item.hidden_​​field :tax_included, :value => p.tax_included %>
<%= p.name %>
$<%= p.price %>

<%结束%>
<%结束%>
<%结束%>

I'm attempting to make an invoice application. Here are my models which are related to my question:

UPDATE: Model information has changed due to recent suggestions

Invoice  
> id  
> created_at  
> sales_person_id

LineItem
> id  
> invoice_id  
> item_id  
> qty_commit (inventory only)  
> qty_sold  
> price (because prices change)  
> ...etc

Item
> barcode  
> name  
> price
> ...etc

Invoice has_many items, :through => :line_items. Ditto for Item. What I want to do is that when I create a new invoice, I'd like the form to be populated with all available Items. The only time I don't want all items to be populated is when I'm viewing the invoice (so only items which exist in the LineItems table should be retrieved). Currently - and obviously - a new Invoice has no items. How do I get them listed when there is nothing currently in the collection, and how do I populate the form? Also I'd like all products to be available when creation fails (along with what the user selected through the form).

UPDATE: I can create items through the controller via the following:

@invoice = Invoice.new
# Populate the invoice with all products so that they can be selected
Item.where("stock > ?", 0).each do |i|
@invoice.items.new(i.attributes)
end

This is of course my crude attempt at doing what I want. Visually it works out great, but as predicted my form id's and such are not playing well when I actually attempt to save the model.

LineItem(#37338684) expected, got Array(#2250012)

An example of the form:

# f is form_for
<% @invoice.items.group_by{|p| p.category}.each do |category, products| %>

<%= category.name %>

<%= f.fields_for :line_items do |line_item| %>
<% for p in products %>

<%= line_item.hidden_field :tax_included, :value => p.tax_included %>
<%= p.name %>
$<%= p.price %>

<% end %>
<% end %>
<% end %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

长亭外,古道边 2024-12-17 20:20:48

首先,如果您明确希望拥有一个包含附加属性的连接模型,则应该使用 has_many :through 而不是 has_and_belongs_to_many。请参阅RoR 差异指南二。

其次,对于您想要达到的目标,没有单一的解决方案。我看到有两种典型用法,具体取决于可能实例的数量,一种比另一种更好:

  1. 使用单选按钮选择(和取消选择)应创建或删除关系的位置。请参阅 railscast #165 如何执行其中部分操作。
  2. 您可以使用带有按钮的选择菜单来添加关系。请参阅 railscast #88。添加的关系可以显示在列表中,附近有一个删除按钮。
  3. 使用令牌字段(请参阅 railscast #258)在一个文本输入字段中自动完成多个条目。

在所有情况下,您通常必须在最后检查是否

  • 关系
  • 应该删除保留
  • 或创建

,我希望一些想法可以向您展示问题的正确解决方案。


First of all, if you explicitly want to have a join model with additional attributes in it, you should use has_many :through instead of has_and_belongs_to_many. See the RoR Guide to the differences of the two.

Second, there is no single solution for what you want to reach. I see there two typical usages, depending on the mass of possible instances, one is better than the other:

  1. Use radio buttons to select (and deselect) where a relation should be created or deleted. See the railscast #165 how to do part of that.
  2. You could use select menus with a button to add a relation. See railscast #88. The added relation could be shown in a list, with a delete button nearby.
  3. Use token fields (see railscast #258) to autocomplete multiple entries in one single text entry field.

In all the situations, you normally have to check at the end, if

  • a relation should be deleted
  • kept
  • or created

I hope some of the ideas may show you the right solution for your problem.


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