有没有办法隐藏模型中的一些嵌套对象?

时间:2010-07-07 08:52:42

标签: ruby-on-rails forms nested

我有一些嵌套项目的订单,这些项目有嵌套种类。当我在视图中执行form_for @order时,我想隐藏其:registered属性设置为 true

的所有项目
<% form_for @order do |f| %>
  <% f.fields_for :items do |ff| %>
    <%# show all the items that have :registered == false %>
  <% end %>
<% end %>

def register
  @order = Order.find(params[:id])
  # Sort out the :registered == true
end

1 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

<% form_for @order do |f| %>
  <% f.fields_for :items do |ff| %>
    <% next if ff.object.registered %>
    <%# show all the items that have :registered == false %>
  <% end %>
<% end %>

或者您可以为未注册的项目创建第二个关联:

class Order < ActiveRecord::Base
  has_many :unregistered_itmes, :class_name => "Item", :conditions => { :registered => false } 
end

然后你可以这样做:

<% form_for @order do |f| %>
  <% f.fields_for :unregistered_items do |ff| %>
    <%# show all the items that have :registered == false %>
  <% end %>
<% end %>