English 中文(简体)
3. 铁路复杂、有3个模型的密封表格
原标题:Rails complex nested forms with 3 models

This question concerns three models:

Sale

class Sale < ActiveRecord::Base
  has_many :sale_items
  has_many :items, through :sale_items
end

Item

class Item < ActiveRecord::Base
  has_many :sale_items
  has_many :sales, :through => :sale_items
end

SaleItem

class SaleItem < ActiveRecord::Base
  belongs_to :sale
  belongs_to :item
end

为解释,<代码>项目作为<代码>sale_item的基础模板。 该申请有许多<代码>Item,但不一定是每个<代码>S的一部分。 因此,sale_item.name 实际点为<代码>sale_item.name 和sale_items 方法如下:

def price
  super || item.price
end

A sale_item either gets its price from its item, or that price can be overridden for that specific sale_item by editing its price column in the database.

这就是我在<条码>中遇到的困难。 观点:我基本上需要一份所有<代码>Item的物体一览表,这些物体见本表的Tinkerbin:

因此,这意味着,如果对一个未加检查的检查箱进行核对并提交表格,就必须建立一个新的<代码>(SaleItem>,并有一个<编码>项目<>>。 等于,如果从清单中加上适当的<代码>price和_quantity领域(quantitySaleItem的具体规定,且不存在Item

此外,如果正在编辑的<代码>Sale已经包括一个特定的<代码>SaleItem,在发出表格时,该核对箱就应当检查(这样一行的箱子就删除<代码>)。 页: 1 www.un.org/spanish/ga/president

我不敢确定如何做到这一点——也许我会从一开始就错过。 我赞成完全取消<代码>SaleItem模型,并只是创设一个<代码>项目_sales的表格,其标题是sale_id>、pricequantity,但我并不相信,这是最佳模式。


Update

我的先前解决办法最后存在一些缺陷和失败测试。 最后,我表示,它将很快找到真正的解决办法。

最佳回答

你们想要的是一个检查箱,有一阵列,在提交文件时,将包含所有选定的id,而不是使用检查箱:

<%= check_box_tag  sale_item_ids[] , item.id -%>

在提交材料时,寄生虫将持有选定的物品。 现在,你们需要做的是就其中每一个问题展开辩论,并适当建立关系(项目)。 你们还必须支持那些已经存在出售的产品,如果这些产品不出现在提交文件的地方,则删除。

在创建实际的html网页时,如果检查箱的贴面已经出售和检查/检查。

希望:

问题回答

如果你对待加入书更像正常铁路模式。

class SaleItems < ActiveRecord::Migration
def change
create_table :sale_items do |t|
  t.integer :sale_id
  t.integer :item_id
  t.float :price
  t.float :quantity

  t.timestamps
   end
  end
end

然后,你可以做这样的事情。

控制员

@sale = Sale.new
@saleitem = @sale.SaleItem.build

You can add a item select for every line in the form. With some javascript you can update the price field from the item selected and change it if you want.

http://railscasts.com/episodes/102-auto-complete-association-revised”rel=“nofollow” (如果你没有铁路预报的亲子!) :here

For adding lines til the Sale form take a look at this railscast on complex forms





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...