fields_for not showing
Hey, absolutely loving Trestle!
I'm stuck with something that should work.. The following code does not show any nested fields.
class Customer < ApplicationRecord
has_one :address, dependent: :destroy
accepts_nested_attributes_for :address
form do |customer|
text_field :name
text_field :email
fields_for :address do
text_field :number_and_street
text_field :suburb
text_field :state
text_field :country
text_field :post_code
end
end
I've had a look at #19 and still lost. Any help would be appreciated!
This happens when your Customer object doesn't yet have an associated Address.
To fix this, you can supply an extra argument to fields_for:
fields_for :address, customer.address || customer.build_address do
Thanks, that makes sense.
Now getting a TypeMismatch error.
ActiveRecord::AssociationTypeMismatch in CustomersAdmin::AdminController#create
Address(#70284209767180) expected, got {"number_and_street"=>"Test", "suburb"=>"Test",
"state"=>"Test", "country"=>"Test", "post_code"=>"Test"} which is an instance of
ActiveSupport::HashWithIndifferentAccess(#70284156005000)
As long as you have the accepts_nested_attributes_for :address line in your Customer model, then Rails should be automatically converting the input field names to customer[address_attributes][field]. It sounds like the input names are coming through as customer[address][field] which will cause that error.
I would double-check that the accepts_nested_attributes_for line is in place and that there are no typos in the association name. You might also want to try restarting the Rails server in case there is a code reloading issue at play.