RoR 3创建发票应用程序 - 如何为HABTM发票/产品关联创建表单? [英] RoR 3 Creating an Invoice app - How do I create the form for a HABTM invoice/products association?

查看:194
本文介绍了RoR 3创建发票应用程序 - 如何为HABTM发票/产品关联创建表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

更新:模型信息因最近的建议而改变

 发票
> id
> created_at
> sales_person_id

LineItem
> id
> invoice_id
> item_id
> qty_commit(仅限库存)
> qty_sold
>价格(因为价格变化)
> ...等

物品
>条码
>名称
>价格
> ... $

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

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


@invoice = Invoice.new

#使用所有产品填充发票,以便它们可以选择

Item.where(stock>?,0).each do | i |

@ invoice.items.new(i.attributes)

结束

这当然是我尝试做我想做的尝试。从视觉上看,它的效果非常好,但是正如我预测的那样,当我实际尝试保存模型时,表单ID等效果并不理想。


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

一个例子:


#f是form_for

<%@ invoice.items.group_by {| p | p.category}。每个do | category,products | %>



<%= category.name%>



<%= f.fields_for:line_items do | LINE_ITEM | %>

<%for p in product%>



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

<%= p.name%>

$<%= p.price%>



< %end%>

<%end%>

<%end%>



has_many:through 而不是 has_and_belongs_to_many 。请参阅 RoR指南两个。

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


  1. 使用单选按钮选择并取消选择)应在哪里创建或删除关系。请参阅 railscast#165 如何做到这一点。
  2. >
  3. 您可以使用带按钮的选择菜单添加关系。请参阅 railscast#88 。添加的关系可以显示在列表中,并在附近有一个删除按钮。

  4. 使用标记字段(请参阅 railscast#258 )在一个文本输入字段中自动填充多个条目。

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


  • 关系应该被删除

  • 保留

  • 或创建



我希望有些想法可能会向您显示您的问题的正确解决方案。





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 %>

解决方案

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.


这篇关于RoR 3创建发票应用程序 - 如何为HABTM发票/产品关联创建表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆