Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is accepts_nested_attributes_for not working for me? (rails 3)
    primarykey
    data
    text
    <p>I'm using formtastic and haml on a Rails 3 application. I'm trying to make a nested form for surveys and questions, but it's just not working for me. I've watched the railscast and on it and everything, but I can't seem to make it work for my application. So right now, I have the following:</p> <p><strong>models</strong></p> <pre><code>class Survey &lt; ActiveRecord::Base attr_accessible :intro, :name, :pubdate, :enddate, :pubid belongs_to :user has_many :questions, :dependent =&gt; :destroy, :autosave =&gt; true accepts_nested_attributes_for :questions, :allow_destroy =&gt; true end class Question &lt; ActiveRecord::Base belongs_to :survey has_many :answers, :dependent =&gt; :destroy attr_accessible :q_text, :order, :q_type end </code></pre> <p><strong>relevant controller method</strong></p> <pre><code>def update @survey = Survey.find(params[:id]) @user = current_user if check_auth_and_redirect @user, @survey if @survey.update_attributes(params[:survey]) flash[:success] = "Survey Updated" redirect_to edit_survey_path(@survey) else @title = "Editing Survey #{@survey.id}" render 'edit' end end end </code></pre> <p><strong>views</strong></p> <pre><code>= semantic_form_for @survey do |f| = render "shared/survey_inputs", :object =&gt; f = f.inputs :for =&gt; :questions, :name =&gt; "Survey Questions" do |fq| %hr = fq.input :q_text, :label =&gt; "Question text" = fq.input :q_type, :label =&gt; "Question type", :as =&gt; :select, :collection =&gt; %w(text scale radio select) = fq.input :order, :label =&gt; "Question order" </code></pre> <p>The form renders correctly, but when I change a question and click save, the records doesn't reflect my changes. I do have debug(params) turned on, and here is what it comes back as:</p> <pre><code>--- !map:ActiveSupport::HashWithIndifferentAccess utf8: "\xE2\x9C\x93" _method: put authenticity_token: CJCc9LvdoPjxwGJkhUnZjR0Z/c5Wt5VBT3bBr/wB4+A= survey: !map:ActiveSupport::HashWithIndifferentAccess name: This is my survey intro: I've got a lovely bunch of coconuts. There they are all standing in a row. pubid: smargdab pubdate(1i): "" pubdate(2i): "" pubdate(3i): "" enddate(1i): "" enddate(2i): "" enddate(3i): "" questions_attributes: !map:ActiveSupport::HashWithIndifferentAccess "0": !map:ActiveSupport::HashWithIndifferentAccess q_text: one one one one one one one one one one one q_type: text order: "3" id: "1" "1": !map:ActiveSupport::HashWithIndifferentAccess q_text: 2 2 2 2 2 2 2 2 2 2 q_type: text order: "1" id: "2" "2": !map:ActiveSupport::HashWithIndifferentAccess q_text: 3 3 3 3 3 3 3 3 3 3 q_type: text order: "2" id: "3" commit: Update Survey action: update controller: surveys id: "2" </code></pre> <p>What am I doing wrong here? I don't want to have to write these attribute changers manually!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload