Rails 3 HABTM дополнительное поле - PullRequest
0 голосов
/ 21 января 2012

Я получил эти модели.Я хочу добавить дополнительное поле, поэтому я использую этот подход вместо 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 (with: through) и: own_to, чтобы заставить его работать.

Редактировать: Мой вопрос: как мне сгенерировать соответствующий text_field_tag ​​для количества?

...