Guys in my system I have Products. And the products have min/max stock for each store. In app/views/product/edit I'm trying to create a way to update it. So i'm trying something like that:
<%= form_for @product.product_stores, :url => {:action => "update_stocks"} do |form| %>
<% @product.product_stores.each do |e| %>
<strong><%= e.store.name %></strong> <br/>
<%= e.id %> <br/>
<div class="row g-3 align-items-center">
<div class="col-auto">
<%= form.label :min_stock_days, class: "col-form-label" %>
</div>
<div class="col">
<%= form.text_field :min_stock_days, class: "form-control" %>
</div>
</div>
<% end %>
<% end %>
And I 'm getting the error on the screen:
undefined method `to_key' for #<ActiveRecord::Associations::CollectionProxy
[#<ProductStore id: 913, min_stock_days: nil, max_stock_days: nil, product_id: 975, store_id: 24, created_at: "2024-01-17 16:14:53.475034000 -0300", updated_at: "2024-01-17 16:14:53.475034000 -0300">,
#<ProductStore id: 11617, min_stock_days: nil, max_stock_days: nil, product_id: 975, store_id: 1, created_at: "2024-01-17 16:58:08.503417000 -0300", updated_at: "2024-01-17 16:58:08.503417000 -0300">,
#<ProductStore id: 11618, min_stock_days: nil, max_stock_days: nil, product_id: 975, store_id: 2, created_at: "2024-01-17 16:58:08.506141000 -0300", updated_at: "2024-01-17 16:58:08.506141000 -0300">,
#<ProductStore id: 11619, min_stock_days: nil, max_stock_days: nil, product_id: 975, store_id: 3, created_at: "2024-01-17 16:58:08.508833000 -0300", updated_at: "2024-01-17 16:58:08.508833000 -0300">,
#<ProductStore id: 11620, min_stock_days: nil, max_stock_days: nil, product_id: 975, store_id: 4, created_at: "2024-01-17 16:58:08.511535000 -0300", updated_at: "2024-01-17 16:58:08.511535000 -0300">,
#<ProductStore id: 11621, min_stock_days: nil, max_stock_days: nil, product_id: 975, store_id: 5, created_at: "2024-01-17 16:58:08.514277000 -0300", updated_at: "2024-01-17 16:58:08.514277000 -0300">,
#<ProductStore id: 11622, min_stock_days: nil, max_stock_days: nil, product_id: 975, store_id: 6, created_at: "2024-01-17 16:58:08.516974000 -0300", updated_at: "2024-01-17 16:58:08.516974000 -0300">,
#<ProductStore id: 11623, min_stock_days: nil, max_stock_days: nil, product_id: 975, store_id: 7, created_at: "2024-01-17 16:58:08.519660000 -0300", updated_at: "2024-01-17 16:58:08.519660000 -0300">,
#<ProductStore id: 11624, min_stock_days: nil, max_stock_days: nil, product_id: 975, store_id: 23, created_at: "2024-01-17 16:58:08.522390000 -0300", updated_at: "2024-01-17 16:58:08.522390000 -0300">]>
Did you mean? to_set
to_ary
How can I fix it? What is the correct way to implement that?
I solved it in parts, thanks for @Arup Rakshit.
My Solution was: in models/product.rb I add
accepts_nested_attributes_for :product_stores, update_only: true
in products_controller.rb I add in product_params
product_stores: [:min_stock_days, :max_stock_days]
and in the view a change to:
<%= form_for @product, as: :post, method: :post, :url => {:action => "update_stocks"} do |form| %>
<%= form.fields_for :product_stores, @product.product_stores.each do |ps|%>
<strong> <%= ps %> </strong>
<div class="row g-3 align-items-center">
<div class="col-auto">
<%= ps.label :min_stock_days, class: "col-form-label" %>
</div>
<div class="col">
<%= ps.text_field :min_stock_days, class: "form-control" %>
</div>
</div>
<div class="row g-3 align-items-center">
<div class="col-auto">
<%= ps.label :max_stock_days, class: "col-form-label" %>
</div>
<div class="col">
<%= ps.text_field :max_stock_days, class: "form-control" %>
</div>
</div>
<% end %>
<div>
<%= form.submit "Salvar", class: "btn btn-primary" %>
</div>
<% end %>
Now, I still having a problem that is... I dont know each min/max field is for whats store? Because I dont know the name of store that i'm seting.
Solution:
My Solution was: in models/product.rb I add
accepts_nested_attributes_for :product_stores, update_only: true
in products_controller.rb I add in product_params
product_stores: [:min_stock_days, :max_stock_days]
and in the view a change to:
<%= form_for @product, as: :post, method: :post, :url => {:action => "update_stocks"} do |form| %>
<%= form.fields_for :product_stores, @product.product_stores.each do |ps|%>
<strong> <%= ps.object.store.name %> </strong>
<div class="row g-3 align-items-center">
<div class="col-auto">
<%= ps.label :min_stock_days, class: "col-form-label" %>
</div>
<div class="col">
<%= ps.text_field :min_stock_days, class: "form-control" %>
</div>
</div>
<div class="row g-3 align-items-center">
<div class="col-auto">
<%= ps.label :max_stock_days, class: "col-form-label" %>
</div>
<div class="col">
<%= ps.text_field :max_stock_days, class: "form-control" %>
</div>
</div>
<% end %>
<div>
<%= form.submit "Salvar", class: "btn btn-primary" %>
</div>
<% end %>
special note here: <strong> <%= ps.object.store.name %> </strong>
When create a form_for
or when you use fields_for
in a form it creates a ActionView::Helpers::FormBuilder
that is a class that extends the Object we are using, so. in my case ps
is an ActionView::Helpers::FormBuilder
extending my class ProductStore. So to know witch store i'm filing the data min/max stock I do that ps.object
(now it is my object) so I complete using .store.name