【问题标题】:Reject_if not working拒绝_如果不工作
【发布时间】:2011-09-19 12:04:52
【问题描述】:

:reject_if 有一些问题。我不知道为什么下面的代码不起作用。

查看 - _form.html.erb:

<%= f.fields_for :questions do |builder| %>
  <div class="nested-field">
    <%= builder.label :id, "Question" %><br />
    <%= builder.collection_select(:id, Question.all(:order => 'question'), :id, :question, { :prompt => 'Select Question' } ) %>
  </div>
  <div class="nested-field">
    <%= builder.label :test1 %><br />
    <%= builder.text_field :test1 %>
  </div>
  <div class="nested-field">
    <%= builder.label :test2 %><br />
    <%= builder.text_field :test2 %>
  </div>
  <div class="nested-field">
    <%= builder.label :description %><br />
    <%= builder.text_field :description %>
  </div>
  <br /><br /><br />
  <hr />
<% end %>

模型 - questionary.rb:

has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:id].blank? }, :allow_destroy => true

非常感谢!

【问题讨论】:

    标签: ruby-on-rails-3 model nested-forms


    【解决方案1】:

    而不是

    :reject_if => lambda { |a| a[:id].blank? }
    

    试试

    :reject_if => lambda { |a| a['id'].blank? }
    

    (注意字符串 'id')并完全遵循 API,请使用 proc

    :reject_if => proc { |a| a['id'].blank? }
    

    【讨论】:

      【解决方案2】:

      我查看了API for accepts_nested_attributes_for,发现那里的文档指出:

      :reject_if 允许您指定 Proc 或 Symbol 指向检查是否应为某个属性哈希构建记录的方法。

      您是否尝试过用 proc 替换 lambda?这里的语法似乎很特殊,所以 lambda 可能会被忽略。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多