Note that there are some explanatory texts on larger screens.

plurals
  1. POneeds nested_form example
    primarykey
    data
    text
    <p>I give up. I'm trying to build a simple nested form with 2 models following Railscasts #196 episode and doesn't work. Can someone send a working example please so I can test on my environment. I'm using 3.1.0</p> <p>For example when I try to build 3 questions on the form only 1 question field appears, then survey_id is never passed across. </p> <p>I would appreciate your help after 2 days and nights on it. I got missing something really big. Thanks</p> <p>Model</p> <pre><code>class Survey &lt; ActiveRecord::Base has_many :questions, :dependent =&gt; :destroy accepts_nested_attributes_for :questions attr_accessible :name, :questions_attributes end class Question &lt; ActiveRecord::Base belongs_to :survey attr_accessible :survey_id, :name end </code></pre> <p>Controller</p> <pre><code>def new @survey = Survey.new 4.times { @survey.questions.build } respond_to do |format| format.html # new.html.erb format.json { render json: @survey } end end def create @survey = Survey.new(params[:survey]) respond_to do |format| if @survey.save format.html { redirect_to @survey, notice: 'Survey was successfully created.' } format.json { render json: @survey, status: :created, location: @survey } else format.html { render action: "new" } format.json { render json: @survey.errors, status: :unprocessable_entity } end end end </code></pre> <p>View</p> <pre><code>&lt;%= form_for(@survey) do |f| %&gt; &lt;% if @survey.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@survey.errors.count, "error") %&gt; prohibited this survey from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @survey.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="field"&gt; &lt;%= f.label :name %&gt;&lt;br /&gt; &lt;%= f.text_field :name %&gt; &lt;/div&gt; &lt;%= fields_for :questions do |builder| %&gt; &lt;%= builder.label :name, "Question" %&gt; &lt;%= builder.text_field :name %&gt; &lt;% end %&gt; &lt;br /&gt;&lt;br /&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre>
    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.
    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