[英]RoR 3 Creating an Invoice app - How do I create the form for a HABTM invoice/products association?
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
. 发票
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).
我唯一不希望填充所有项目的时间是在查看发票时(因此,仅应检索LineItems表中存在的项目)。 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
@invoice =发票。新
# Populate the invoice with all products so that they can be selected#用所有产品填充发票,以便可以选择它们
Item.where("stock > ?", 0).each do |i|Item.where(“ stock>?”,0).each做| i |
@invoice.items.new(i.attributes)@ 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.
在外观上效果很好,但是正如我所预测的那样,当我实际尝试保存模型时,这样的表单ID不能很好地发挥作用。
LineItem(#37338684) expected, got Array(#2250012)
预期LineItem(#37338684),得到了Array(#2250012)
An example of the form: 形式的示例:
# f is form_for
#f是form_for
<% @invoice.items.group_by{|p|<%@ invoice.items.group_by {| p | p.category}.each do |category, products|
p.category}。每个|类别,产品| %>
%>
<%= category.name %><%= category.name%>
<%= f.fields_for :line_items do |line_item|<%= f.fields_for:line_items做| line_item | %>
%>
<% for p in products %><产品中p的百分比%>
<%= line_item.hidden_field :tax_included, :value => p.tax_included %><%= line_item.hidden_field:tax_included,:value => p.tax_included%>
<%= p.name %><%= p.name%>
$<%= p.price %>$ <%=价格%>
<% end %><%结束%>
<% end %><%结束%>
<% end %><%结束%>
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
. 首先,如果您明确希望拥有一个具有附加属性的
has_and_belongs_to_many
模型,则应使用has_many :through
而不是has_and_belongs_to_many
。 See the RoR Guide to the differences of the two. 有关这两者的区别 ,请参阅RoR指南 。
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. 我希望某些想法可以为您提供解决问题的正确方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.