If statement 是否对嵌套窗体上的属性计数?

If statement 是否对嵌套窗体上的属性计数?,if-statement,count,nested-attributes,If Statement,Count,Nested Attributes,Rails 3.2.12和Ruby 1.9.3以及Haml 我想使用count of attribute来控制“link_to“remove”的显示,但是我在设置逻辑时遇到了问题 以下是我的表单中的一些代码: .field = codeline.label :name, "Units Alloc" %br/ = codeline.text_field :units_alloc, :precision => 6, :scale => 2, :

Rails 3.2.12和Ruby 1.9.3以及Haml

我想使用count of attribute来控制“link_to“remove”的显示,但是我在设置逻辑时遇到了问题

以下是我的表单中的一些代码:

    .field
      = codeline.label :name, "Units Alloc"
      %br/
      = codeline.text_field :units_alloc, :precision => 6, :scale => 2, :size => 10,
       :class => "ui-state-default"
      = codeline.hidden_field :_destroy
      = link_to "remove", '#', class: "remove_fields"
这工作得很好,但我有'删除'链接显示,我希望它只显示,如果有两个:单位(alloc)属性

这就是我所尝试的:

    .field
      = codeline.label :name, "Units Alloc"
      %br/
      = codeline.text_field :units_alloc, :precision => 6, :scale => 2, :size => 10,
       :class => "ui-state-default"
      - if :units_alloc.count > 1
      = codeline.hidden_field :_destroy
      = link_to "remove", '#', class: "remove_fields"
这是我的错误:

       NoMethodError in Contracts#new

       Showing /home/tom/rails_projects/tracking/app/views/contracts
       /_codeline_fields.html.haml where line #9 raised:

       undefined method `count' for :units_alloc:Symbol
如果我在参数中使用units_alloc而不是符号,我仍然会得到一个错误:

        NameError in Contracts#new

        Showing /home/tom/rails_projects/tracking/app/views/contracts
        /_codeline_fields.html.haml where line #9 raised:

        undefined local variable or method `units_alloc' for
        #<#<Class:0xadbde90>:0xa8956e8>
app/views/contracts/_codelines_fields.html.haml

   .field
      = codeline.label :name, "Units Alloc"
      %br/
      = codeline.text_field :units_alloc, :precision => 6, :scale => 2, :size => 10,
       :class => "ui-state-default"
      - if @show_remove
          = codeline.hidden_field :_destroy
          = link_to "remove", '#', class: "remove_fields"
      - else
         - @show_remove = true

就这样。。。“删除”按钮仅显示在属性的第二行和后续行中。

当您在表单(部分)中时,代码行并不引用表单(部分)所针对的代码行实例,但是一个简单的ActionView::Helpers::FormBuilder实例知道如何将信息与代码行实例关联。您知道这一点,因为在部分的第一行中,您有
codeline.object.build\u code


因此,如果您想访问有关单位分配的信息,可以使用
codeline.object.units\u分配
访问它们。这将为您提供条件的数据。

我只想补充一点,如果锚标记的目的是使用一些javacscript从表单列表中删除元素,那么您可能使用了错误的控件。锚定标记不是表单元素,它们应该指向资源/内容,而不是用作动画/客户端行为触发器。根据您描述的用例,对于您似乎要实现的目标,输入标记type=按钮将是一个更合适的元素

感谢James Scott Jr的解决方案:谢谢@Chuck。。。我将不得不点燃灰色细胞来欣赏这里的细微差别。。。通过一些研究,我很可能会非常欣赏你的观点。刚才注意到编辑器没有考虑我的html标记。我指的是输入标签,类型=按钮
   .field
      = codeline.label :name, "Units Alloc"
      %br/
      = codeline.text_field :units_alloc, :precision => 6, :scale => 2, :size => 10,
       :class => "ui-state-default"
      - if @show_remove
          = codeline.hidden_field :_destroy
          = link_to "remove", '#', class: "remove_fields"
      - else
         - @show_remove = true