2
votes

Noob question, I'm sure, but I can't seem to find my mistake. SymptomSets are saving w/ the proper user_id, but the nested symptoms disappear. Note that the user model structure is identical to that in the Rails Tutorial (save that it 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 do
        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 the View:

<%= simple_form_for @symptom_set, :html => { :class => 'form-inline' } do |f| %>

    <%= f.fields_for :symptoms do |builder| %>
   <%= render 'symptom_fields', f: builder %>
    <% end %>

    <div class="actions"><%= f.submit %></div>
<% end %>

And the partial:

       <%= f.input :name, 
                   :collection=> ["Cough", "Fever", "Headache", "Lethargy"], 
                    label: "Symptom", 
                    prompt: "Select a symptom",
                   :input_html => { :class => "span3" }%>  

       <%= f.input :duration, 
                   :collection => 1..14, 
                    label: "Duration",
                    prompt: "How many days?" %>

finally, the rails server console outputs the 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".* FROM "users" WHERE "users"."remember_token" ='OH6_nuvySNjd6AbTuDunsw' LIMIT 1

(0.1ms) BEGIN SQL (0.4ms) INSERT INTO "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

1

1 Answers

0
votes

I'd try 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 would work there.

And also checking the params on the terminal log if it is called symptom_sets and if it's sending the parameters of nested form attributes.

EDIT:

I Really think your param's name would be symptom_set in singular. check that.