Rails 4 nested form not loading values ββin show / edit
I have an order form with a nested form for products. Products are saved in the database correctly, but when displaying / editing product values, they are not loaded - fields are displayed, but not filled.
order model:
has_many :products, dependent: :destroy
accepts_nested_attributes_for :products
product model:
belongs_to :order
orders_controller:
before_action :set_order, only: [:show, :edit]
def new
@order = Order.new
@order.products.build
end
def show
# empty
end
def edit
# empty
end
def order_params
params.require(:order).permit(
:number,
:price,
products_attributes: [
:id,
:type,
:color
])
end
def set_order
if params[:id].present? && user_signed_in?
@order = Order.find(params[:id])
elsif params[:number].present? && params[:email].present?
@order = Order.find_by(number: params[:number], email: params[:email])
else
redirect_to welcome_index_path, alert: I18n.t('views.welcome.index.no_order_found')
end
end
routes.rb:
resources :orders do
get 'express', on: :new
end
post 'orders/status', to: 'orders#show'
_form.html.erb (used for both editing and display):
<%= form_for @order do |f| %>
<%= f.text_field(:number) %>
# etc.
<%= f.fields_for :products do |g| %>
<%= g.text_field(:color) %>
# etc.
<% end %>
<% end %>
source to share
I finally figured out the problem - it was in my regular FormBuilder class. I didn't include it in the original question because I didn't think it mattered. My form looks something like this:
<%= f.fields_for :products, builder: MyFormBuilder do |g| %>
<%= g.product_select(:color) %>
<%= g.product_checkbox(:accessories) %>
# etc.
<% end %>
MyFormBuilder had the following methods:
def product_select(label)
tags = ''
Product.send(label.to_s.pluralize).each do |a|
tags += @template.content_tag(:option, a[:text], value: a[:value], data: a[:data])
end
tag_with_label(label) do
@template.select(@object_name, label, nil, {}, options) { tags.html_safe }
end
end
def product_checkbox(label)
tag_with_label("#{label.to_s}?") do
@template.check_box(@object_name, label)
end
end
The problem was that nothing was specified on how to load the stored attributes for an existing object. Here are the fixed versions of the methods:
def product_select(label)
choices = @template.options_for_select(Product.send(label.to_s.pluralize), @object.send(label))
tag_with_label(label) do
@template.select(@object_name, label, choices, {}, options)
end
end
def product_checkbox(label)
tag_with_label("#{label.to_s}?") do
@template.check_box_tag("#{@object_name}[#{label}]", 1, @object.send(label))
end
end
The key part was @object.send(label)
, which was a method call to retrieve the appropriate attribute from the database. I also changed the product model to return an array in the format expected by options_for_select (array of arrays).
source to share
Your code seems to be good and it should work. You can try the following change to pass @products
to form fields
<%= f.fields_for :products, @products do |g| %>
<%= g.text_field(:color) %>
# etc.
<% end %>
And just create a variable @products
available in the action new
and edit
. As after
def new
#your code goes here ...
@products = @order.products.build
end
def edit
#your code goes here ...
@products = @order.products
end
source to share