Note that there are some explanatory texts on larger screens.

plurals
  1. PORails3: Nested form doesn't save because id
    primarykey
    data
    text
    <p>The two Models (Step contains "accepts_nested_attributes_for") :</p> <pre><code>class Step &lt; ActiveRecord::Base has_many :statements accepts_nested_attributes_for :statements end class Statement &lt; ActiveRecord::Base belongs_to :step end </code></pre> <p>Step Controller, methods new (with @step.statements.build) and create:</p> <pre><code>def new @step = Step.new @step.statements.build respond_to do |format| format.html # new.html.erb format.json { render json: @step } end end def create @step = Step.new(params[:step]) respond_to do |format| if @step.save format.html { redirect_to @step, notice: 'Step was successfully created.' } format.json { render json: @step, status: :created, location: @step } else format.html { render action: "new" } format.json { render json: @step.errors, status: :unprocessable_entity } end end end </code></pre> <p>and the nested form in New view with form_fields for statements:</p> <pre><code>&lt;%= form_for(@step) do |step_form| %&gt; &lt;div class="field"&gt; &lt;%= step_form.label :step_type %&gt;&lt;br /&gt; &lt;%= select("step", "step_type_id", @step_types.collect {|p| [ p.name, p.id ] }, {:include_blank =&gt; true}) %&gt; &lt;/div&gt; &lt;%= step_form.fields_for :statements do |statement_form| %&gt; &lt;div class="field"&gt; &lt;%= statement_form.label :title %&gt;&lt;br /&gt; &lt;%= statement_form.text_field :title %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="actions"&gt; &lt;%= step_form.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>when submit the models don't save because: "Statements step can't be blank" (step should be create before...)</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.
    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