简体   繁体   English

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

[英]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: 我看到两种典型用法,具体取决于可能的实例量,一种优于另一种:

  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. 参见railscast#165如何做到这一点。
  2. You could use select menus with a button to add a relation. 您可以使用带有按钮的选择菜单来添加关系。 See railscast #88 . 参见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. 使用令牌字段(请参见railscast#258 )在一个文本输入字段中自动完成多个条目。

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. 我希望某些想法可以为您提供解决问题的正确方法。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM