Rails: Multi-model form won't write data to models

133 Views Asked by At

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:

class Outfit < 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 => :true
  accepts_nested_attributes_for :answers

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.

I've created a multi-model form for this data which I want to publish with one button. Here is the code:

<%= form_for(@outfit) do |post_outfit| %>

  <%= post_outfit.fields_for @outfit.outfit_relationships do |build| %>
    <table>
     <tr>
        <td>X Coord <%= build.text_area :image_x_coord, :size => '1x1' %></td>
        <td>Y Coord <%= build.text_area :image_y_coord, :size => '1x1' %></td>
        <td>Z Index <%= build.text_area :zindex, :size => '1x1' %></td>
        <td>Height <%= build.text_area :image_size_height, :size => '1x1' %></td>
        <td>Weight <%= build.text_area :image_size_width, :size => '1x1' %></td>
     </tr>
    </table>
  <% end %>

  <%= post_outfit.fields_for @outfit.answers do |build| %></br></br>
    <%= image_tag current_user.fbprofileimage, :size => "40x40" %></br>
    <%= current_user.name %></br>
    Comment: <%= build.text_area :description, :size => '10x10' %>
  <% end %>

  <%= post_outfit.fields_for @outfit do |build| %> </br>
   origin id: <%= build.text_area :outfit_origin_id, :size => '1x1' %></br>
   parent id:  <%= build.text_area :outfit_parent_id, :size => '1x1' %></br>
  <% end %>     

  <div id="ss_QID_actions_container">
    <%= post_outfit.submit "Submit checked", :class => "btn btn-small btn-primary" %>
  </div>
<% end %>

And here are the relevant buts of the outfit controller:

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)

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.

0

There are 0 best solutions below