RoR 3 创建发票应用程序 - 如何创建 HABTM 发票/产品关联的表单?
我正在尝试申请发票。以下是与我的问题相关的模型:
更新:由于最近的建议,模型信息已更改
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,如果您明确希望拥有一个包含附加属性的连接模型,则应该使用
has_many :through
而不是has_and_belongs_to_many
。请参阅RoR 差异指南二。其次,对于您想要达到的目标,没有单一的解决方案。我看到有两种典型用法,具体取决于可能实例的数量,一种比另一种更好:
在所有情况下,您通常必须在最后检查是否
,我希望一些想法可以向您展示问题的正确解决方案。
First of all, if you explicitly want to have a join model with additional attributes in it, you should use
has_many :through
instead ofhas_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:
In all the situations, you normally have to check at the end, if
I hope some of the ideas may show you the right solution for your problem.