Rails 3 HABTM额外字段

时间:2012-01-20 23:09:27

标签: ruby-on-rails ruby-on-rails-3 has-and-belongs-to-many

我有这些模特。想要添加一个额外的字段,所以我使用这种方法而不是HABTM(实际上有效,但没有额外字段'数量')

class PointOfSale < ActiveRecord::Base
  has_many :items
  has_many :inventories, :through => :items
  accepts_nested_attributes_for :items
end

class Inventory < ActiveRecord::Base
  has_many :items
  has_many :point_of_sales, :through => :items
end

class Item < ActiveRecord::Base
 belongs_to :inventory
 belongs_to :point_of_sale
 #Quantity here
end

在我的控制器中,我得到了:

def assign_inventory
  @point_of_sale = PointOfSale.find(params[:point_of_sale_id])
  @items = @point_of_sale.inventories.build
  @distributor = @point_of_sale.distributor
  @inventories = Inventory.where("status = ?", 1)
end

def post_assign_inventory
  @point_of_sale = PointOfSale.find(params[:point_of_sale_id])
  @distributor = @point_of_sale.distributor
  @point_of_sale.update_attributes(params[:point_of_sale])
  respond_to do |format|
    format.html { redirect_to  distributor_point_of_sale_path(params[:distributor_id],@point_of_sale.id) }
  end
end

现在我有以下形式:

<%= form_for([@distributor,@point_of_sale,@items]) do |f| %>
<% for inventory in @inventories %>
    <div class="item">
        <%= hidden_field_tag "point_of_sale[inventory_ids][]", "" %>
        <%= check_box_tag "point_of_sale[inventory_ids][]", inventory.id, @point_of_sale.inventories.include?(inventory) %>
        <%= inventory.article_name %>,
        Quantity: **<%= f.text_field :inventory.item.quantity %>**<br />
    </div>
<% end %>
<div>
    <%= f.submit(:value => "Save", :disable_with => "Saving...") %>
</div>
<% end %>

复选框的作用就像一个魅力,但我不知道如何添加额外的字段,寻找这个,但只发现我必须添加:has_many(使用:through)和:belongs_to使其工作。< / p>

编辑: 我的问题是,我应该如何在数量上生成适当的text_field_tag?

0 个答案:

没有答案