Reputation: 2453
This is my first time messing with nested attributes. I'm having an issue where I am trying to create a 'School' that has a 'Token' and its attributes. Upon submitting the form, I'll get errors saying 'The Token attributes cannot be blank' (b/c of my model validations) even though I am submitting the form with Token values for the Token attributes.
I think things are misaligned when looking at the server logs but I'm not sure why?
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xbDhfXJotAfgg6O9rnrSuKy01cxoTi/ZpgaDuD4fkQA=", "school"=>{"name"=>"Palmer", "address"=>"123 Palmer lane", "city"=>"Baldwinsville", "state"=>"CA", "zip"=>"10001", "tokens_attributes"=>{"0"=>{"database"=>"Rhetorical studies", "start_date(1i)"=>"2013", "start_date(2i)"=>"8", "start_date(3i)"=>"29", "expiration_date(1i)"=>"2014", "expiration_date(2i)"=>"8", "expiration_date(3i)"=>"29"}}}, "commit"=>"Update"}
My schools model looks like this:
class School < ActiveRecord::Base
has_many :users
has_many :tokens
accepts_nested_attributes_for :tokens
end
The schools_controller's new action looks like the following:
def new
@school = School.new
@school.tokens.build
end
My Schools form has the following fields_for:
<%= f.fields_for :tokens do |builder| %>
<p>
<%= builder.label "Database(s)" %>
<%= builder.text_field :database %>
<p>
<p>
<%= builder.label "Start Date" %><br />
<%= builder.date_select :start_date %>
<p>
<p>
<%= builder.label "Expiration Date" %><br />
<%= builder.date_select :expiration_date %>
<p>
<hr />
<% end -%>
And the 'school_params' strong params in the 'schools_controller.rb' look like this:
def school_params
params.require(:school).permit(:name, :address, :city, :state, :zip, tokens_attributes: [:id, :user_id, :school_id, :database, :start_date, :expiration_date])
end
Based on the logs, it seems as though I'm doing something wrong in 'school_params'. Any thoughts?
Upvotes: 2
Views: 573
Reputation: 2453
Oops. I was working on this late last night. In doing so I had '@school.tokens.build' in both the 'new' and the 'create' actions. Removed it from 'create', and it's working fine now.
Upvotes: 0