0

假设我有三个对象/模型;年、汽车和零件。现在,我有汽车属于_to 零件。对此的想法是,一个零件可能是许多汽车的一部分,但一辆汽车在给定时间只有一个零件。和年有_many汽车通过car_years。我已经成功地将汽车表格嵌套在年份表格中。但是,当我尝试将零件表单嵌套在汽车表单中并创建新的一年时,我无法批量分配受保护的:零件。但是,我可以制造一辆新车,它会成功地制造出新零件。我想知道我是否走在正确的轨道上,或者我应该寻找其他地方来解决这个问题。我从 rails cast #196 得到了这个想法。

年份型号

class Year < ActiveRecord::Base
  attr_accessible :year, :cars_attributes

  has_many :car_years  
  has_many :cars, :through => :machine_years

  accepts_nested_attributes_for :car_years,
    :reject_if => lambda { |a| a[:num_cars].blank? }, 
    :allow_destroy => true 

  accepts_nested_attributes_for :cars,
    :reject_if => lambda { |a| a[:doors].blank? }, 
    :allow_destroy => true 

end

汽车模型

class Machine < ActiveRecord::Base
  attr_accessible ..., :part_id, :part_attributes

  has_many :car_years
  has_many :years, :through => :car_years
  belongs_to :part

  accepts_nested_attributes_for :car_years
  accepts_nested_attributes_for :years

  accepts_nested_attributes_for :part,
    :reject_if => lambda { |a| a[:name].blank? }

end

零件型号

class Part < ActiveRecord::Base
  attr_accessible :desc, :name

  has_many :machines
end

Year_form 视图

<%= form_for(@year) do |f| %>
  <% if @year.errors.any? %>
    <div id="error_explanation">    
      <h2><%= pluralize(@year.errors.count, "error") %> prohibited this year from being saved:</h2>

      <ul>                 
      <% @year.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">      
    <%= f.label :year %><br />
    <%= f.date_select :year, :start_year => 1940, :end_year => Time.now.year, :discard_day => true, :discard_mon    th => true %>
  </div>

    <h3>Cars</h3>      
    <div>
      <%= f.fields_for :cars do |builder| %>
        <%= render 'car_fields', :f => builder %>
      <% end %>            
      <p><%= link_to_add_fields "Add Car", f, :cars %></p>
    </div>                 

  </div>

  <div class="actions">    
    <%= f.submit %>        
  </div>
<% end %>

Car_fields.html.erb

<div class="field">
  <h3>New Car</h3>
  <table>
    <tbody>
      <tr>
        <th><%= f.label :name %></th>
        <th><%= f.label :model %></th>
        <th><%= f.label :part_id %></th>
      </tr>
      <tr>
        <td><%= f.text_field :name %></td>
        <td><%= f.text_field :model %></td>
        <td>
          <%= f.fields_for :part do |builder| %>
            <%= render "cars/parts_fields", :f => builder %>
          <% end %>
        </td>
        <td><%= f.number_field :memory_id %></td>
      </tr>
      <tr>
        <th><%= f.label :install_dt, "Install Date" %></th>
        <th></th>
        <th><%= f.label :production_dt, "Production Date" %></th>
        <th></th>
        <th><%= f.label :remove_dt, "Remove Date" %></th>
      </tr>
      <tr>
        <td><%= f.date_select :install_dt, :start_year => 1940, :end_year => Time.now.year %></td>
        <td></td>
        <td><%= f.date_select :production_dt, :start_year => 1940, :end_year => Time.now.year %></td>
        <td></td>
        <td><%= f.date_select :remove_dt, :start_year => 1940, :end_year => Time.now.year %></td>
      </tr>
    </tbody>
  </table>
  <table>
    <tbody>
      <tr>
        <th><%= f.label :history %></th>
        <th></th>
        <th><%= f.label :design %></th>
        <th></th>
        <th><%= f.label :notes %></th>
      </tr>
      <tr>
        <td><%= f.text_area :history, :rows => 4 %></td>
        <td></td>
        <td><%= f.text_area :design, :rows => 4 %></td>
        <td></td>
        <td><%= f.text_area :notes, :rows => 4 %></td>
      </tr>
     </tbody>
   </table>
  <div style="padding: 2px;">
    <%= f.label :image %><br />
    <%= f.text_field :image %>
  </div>
  <%= link_to_function "remove", "remove_fields(this)" %>
</div>

汽车/part_fields.html.erb

<div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name %><br /> 
  <%= f.label :desc %><br />
  <%= f.text_area :desc, :rows => 3 %>
</div>
4

2 回答 2

0

好的,所以解决方案在另一篇文章中: 让 fields_for 和 Accepts_nested_attributes_for 与 belongs_to 关系一起工作

在我需要调用的 car_fields 表单中

<%= f.fields_for :part_attributes do |builder| %>

不是

<% =f.fields_for :parts do |builder| %>

但是,现在当我查看年份编辑屏幕时,部分字段为空,但 part_type 确实存在

于 2013-06-25T17:52:21.320 回答
0

没有看到任何代码就很难说。但是,以下错误cannot mass assign protected: parts意味着您应该添加attr_accessible :parts到模型中以便能够执行以下操作:

a_part = Part.new
another_part = Part.new
Car.new(parts: [a_part, another_part])

否则,没有它,您将不得不这样做:

c = Car.new
c.parts << a_part
c.parts << another_part
于 2013-06-24T20:58:38.513 回答