Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Multi-model form won't write data to models
    primarykey
    data
    text
    <p>Ok this is driving me round the bend. I have three models [which are relevant to this quesiton]: Outfit, Outfit_relationship and Answer. Outfit is the parent model and the others are the childs. The Outfit model looks like this:</p> <pre><code>class Outfit &lt; ActiveRecord::Base attr_accessible :user_id, :outfit_origin_id, :outfit_parent_id, :outfitrelationship_id #review before going live attr_accessible :item_id, :image_size_height, :image_size_width, :image_x_coord, :image_y_coord, :zindex, :outfit_id attr_accessible :description, :question_id, :user_id, :outfit_id has_many :answers has_many :outfit_relationships accepts_nested_attributes_for :outfit_relationships, :allow_destroy =&gt; :true accepts_nested_attributes_for :answers </code></pre> <p>Note that the 2nd and 3rd attr_accessible are to access the attributes from the other models. I'm not sure this is absolutely necessary, some articles say it is, some say it isn't, so I put it in.</p> <p>I've created a multi-model form for this data which I want to publish with one button. Here is the code:</p> <pre><code>&lt;%= form_for(@outfit) do |post_outfit| %&gt; &lt;%= post_outfit.fields_for @outfit.outfit_relationships do |build| %&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;X Coord &lt;%= build.text_area :image_x_coord, :size =&gt; '1x1' %&gt;&lt;/td&gt; &lt;td&gt;Y Coord &lt;%= build.text_area :image_y_coord, :size =&gt; '1x1' %&gt;&lt;/td&gt; &lt;td&gt;Z Index &lt;%= build.text_area :zindex, :size =&gt; '1x1' %&gt;&lt;/td&gt; &lt;td&gt;Height &lt;%= build.text_area :image_size_height, :size =&gt; '1x1' %&gt;&lt;/td&gt; &lt;td&gt;Weight &lt;%= build.text_area :image_size_width, :size =&gt; '1x1' %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;% end %&gt; &lt;%= post_outfit.fields_for @outfit.answers do |build| %&gt;&lt;/br&gt;&lt;/br&gt; &lt;%= image_tag current_user.fbprofileimage, :size =&gt; "40x40" %&gt;&lt;/br&gt; &lt;%= current_user.name %&gt;&lt;/br&gt; Comment: &lt;%= build.text_area :description, :size =&gt; '10x10' %&gt; &lt;% end %&gt; &lt;%= post_outfit.fields_for @outfit do |build| %&gt; &lt;/br&gt; origin id: &lt;%= build.text_area :outfit_origin_id, :size =&gt; '1x1' %&gt;&lt;/br&gt; parent id: &lt;%= build.text_area :outfit_parent_id, :size =&gt; '1x1' %&gt;&lt;/br&gt; &lt;% end %&gt; &lt;div id="ss_QID_actions_container"&gt; &lt;%= post_outfit.submit "Submit checked", :class =&gt; "btn btn-small btn-primary" %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>And here are the relevant buts of the outfit controller:</p> <pre><code>def new @outfit = Outfit.new @outfit.save @outfit.answers.build @outfit.outfit_relationships.build respond_to do |format| format.html # new.html.erb format.json { render json: @outfit } end end def create @outfit = Outfit.new(params[:id]) @comment = @outfit.answers.create(params[:answer]) @outfitrelationship = @outfit.outfit_relationships.create(params[:outfit_relationship]) redirect_to outfit_path(@outfit) </code></pre> <p>So the problem is nothing gets written into my database apart from the IDs. I'm sure I'm dong something stupid here, but can't figure out why. </p>
    singulars
    1. This table or related slice is empty.
    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