1

好的,这正在驱使我转弯。我有三个模型[与这个问题相关]:Outfit、Outfit_relationship 和 Answer。Outfit 是父模型,其他是子模型。Outfit 模型如下所示:

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

请注意,第二个和第三个 attr_accessible 用于访问其他模型的属性。我不确定这是否绝对必要,有的文章说有,有的说没有,所以我把它放进去。

我为此数据创建了一个多模型表单,我想用一个按钮发布它。这是代码:

<%= 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 %>

以下是装备控制器的相关信息:

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)

所以问题是除了ID之外没有任何东西写入我的数据库。我确定我在这里做一些愚蠢的事情,但不知道为什么。

4

0 回答 0