简体   繁体   中英

How to edit and update attributes in join table in has_many: through association?

I have has_many through join table for Product-Warehouse setup where product and warehouse connect through warehouse_products. warehouse_product includes product_id ,warehouse_id ,item_count and low_threshold.

Question: Going to a product edit page allows to change the item_counts and thresholds of that product at all warehouses.

The edit.htlm.erb looks like :

    <%= f.label 'Product Name' %><br>
    <%= f.text_field :product_name %><br>

    <%= f.label 'Sku_Code' %><br>
    <%= f.text_field :sku_code%><br>

    <%= fields_for :warehouse_products do |i|%>
            <%= i.number_field :item_count %><br>
            <%= i.number_field :low_threshold%><br>
    <% end %>

    <%= submit_tag "Edit Product" %>

The edit and update functions:

   def edit
       @products = Product.find(params[:id])
   end

   def update

       @products = Product.find(params[:id])

       if @products.update_attributes!(product_params)
          redirect_to action: "index", notice: 'Product was successfully updated.' 
       else
          redirect_to action: "edit"
       end

    end

    private

    def product_params
      params.require(:product).permit(warehouse_products_attributes: [:item_count ,:low_threshold])
    end

How do I update the item_count and low_threshold for all the warehouses(Mumbai,Delhi etc).

I am not 100% sure what causes your issue, but you might try this:

In your warehouse model:

has_many :warehouse_products, dependent: :destroy
has_many :products, through: :warehouse_products 
accepts_nested_attributes_for :warehouse_products, reject_if: :all_blank, allow_destroy: true

In your warehouse_product model:

belongs_to :warehouse
belongs_to :product

In your product model:

has_many :warehouse_products, dependent: :destroy
has_many :warehouses, through: :warehouse_products 
accepts_nested_attributes_for :warehouse_products, reject_if: :all_blank, allow_destroy: true

And in your controller's permitted attributes:

warehouse_products_attributes: [:id, :warehouse_id, :item_count, :low_threshold, :_destroy],

Update

Your form:

<%= form_for :product do |f| %>  

  <%= f.label 'Product Name' %>
  <br>
  <%= f.text_field :product_name %>
  <br>

  <%= f.label 'Sku_Code' %>
  <br>
  <%= f.text_field :sku_code%>
  <br>

  <%= fields_for :warehouse_products do |i| %> 

    <% Warehouse.all.each do |warehouse| %>
      <div class="warehouse_fields">
        <label for="warehouse-<%= warehouse.id %>">
          <%= i.radio_button :warehouse_id, warehouse.id, id: "warehouse-#{warehouse.id}", class: "warehouse" %>
          <span>
            <%= warehouse.name %>
          </span>
        </label>
      </div>
    <% end %>

    <br>

    <%= i.label :item_count, 'Item Count' %>
    <br> 
    <%= i.number_field :item_count %>

    <br> 

    <%= i.label 'Low Threshold' %>
    <br> 
    <%= i.number_field :low_threshold %>
  <% end %>

<% end %>

Your controller:

def product_params 
  params.require(:product).permit(
    :sku_code,
    :product_name,
    warehouse_products_attributes: [:id, :warehouse_id, :item_count, :low_threshold, :_destroy]
  )
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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