Rails reject_if无法使用.build?

时间:2014-08-01 18:25:56

标签: ruby-on-rails nested-attributes

我的代码在这里:

model A


has_many :parent_leasing_configurations, :conditions => { :parent_id=> nil }, :class_name=> 'LeasingConfiguration'
accepts_nested_attributes_for :parent_leasing_configurations

并查看文件:

<%= f.fields_for :parent_leasing_configurations do |leasing_configuration| %>
   <tr class="<%= 'tablefbAlternate' if (tr_alter%2==0) %>">
     <td class="bdr-lt-gry bdr-rt-gry bdr-top-gry tdlfpd">
      <% if leasing_configuration.object.new_record? %>
        <%= leasing_configuration.text_field :name, :placeholder=>"Enter New Category" %>
      <% else %>
        <%= leasing_configuration.object.name %>
      <% end %>
    </td>
    <td class="bdr-rt-gry bdr-top-gry"></td>
</tr>

我正在更新contoller:

@current_client.update_attributes(params[:client])

我的问题是reject_if不适用于新记录。每次添加空记录。

对此有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您需要告诉active_record拒绝嵌套属性为空/空。

accepts_nested_attributes_for :parent_leasing_configurations, reject_if: proc { |attributes| attributes['name'].blank? }

有关 nested attributes

的更多信息