Saturday, 15 January 2011

models - Nested form not saving in rails 3.1 -



models - Nested form not saving in rails 3.1 -

noob question, i'm sure, can't seem find mistake. symptomsets saving w/ proper user_id, nested symptoms disappear. note user model construction identical in rails tutorial (save has_many :symptom_sets)

models:

class symptomset < activerecord::base attr_accessible :symptoms, :symptoms_attributes belongs_to :user has_many :symptoms, :dependent => :destroy accepts_nested_attributes_for :symptoms, allow_destroy: true end class symptom < activerecord::base attr_accessible :name, :duration, :symptom_set_id belongs_to :symptom_set end

controller:

class symptomsetscontroller < applicationcontroller before_filter :signed_in_user, only: [:create, :new] def new @symptom_set = symptomset.new 3.times symptom = @symptom_set.symptoms.build end end def create @symptom_set = current_user.symptom_sets.build(params[:symptom_sets]) if @symptom_set.save flash[:success] = "symptoms submitted!" redirect_to root_url else render 'static_pages/home' end end

and view:

<%= simple_form_for @symptom_set, :html => { :class => 'form-inline' } |f| %> <%= f.fields_for :symptoms |builder| %> <%= render 'symptom_fields', f: builder %> <% end %> <div class="actions"><%= f.submit %></div> <% end %>

and partial:

<%= f.input :name, :collection=> ["cough", "fever", "headache", "lethargy"], label: "symptom", prompt: "select symptom", :input_html => { :class => "span3" }%> <%= f.input :duration, :collection => 1..14, label: "duration", prompt: "how many days?" %>

finally, rails server console outputs following:

parameters: {"utf8"=>"✓", "authenticity_token"=>"s7ksuk40m2r76nq4pgeeptpkcecxfnip4ttpfshszqk=", "symptom_set"=>{"symptoms_attributes"=>{"0"=>{"name"=>"cough", "_destroy"=>"false", "duration"=>"2"}, "1 "=>{"name"=>"fever", "_destroy"=>"false", "duration"=>"2"}, "2"=>{"name"=>"", "_destroy"=>"1", "duration"=>""}}}, "commit"=>"create symptom set"} user load (0.4ms) select "users".* "users" "users"."remember_token" ='oh6_nuvysnjd6abtudunsw' limit 1

(0.1ms) begin sql (0.4ms) insert "symptom_sets" ("created_at", "updated_at", "user_id") values ($1, $2, $3) returning "id" [["created_at", tue, 05 feb 2013 21:12:07 utc +00:00], ["updated_at", tue, 05 feb 20 13 21:12:07 utc +00:00], ["user_id", 1]] (1.1ms) commit

i'd seek changing:

@symptom_set = current_user.symptom_sets.build(params[:symptom_sets])

to:

@symptom_set = current_user.symptom_sets.new(params[:symptom_sets])

i don't know if build work there.

and checking params on terminal log if called symptom_sets , if it's sending parameters of nested form attributes.

edit:

i think param's name symptom_set in singular. check that.

ruby-on-rails models nested-forms nested-attributes

No comments:

Post a Comment