Rails 3.2.12和Ruby 1.9.3和Haml计算嵌套窗体上的属性?
我想使用属性的计数来控制'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"
这个作品很好,但我有“删除”链接显示出来,我会喜欢它,只显示如果有两个:units_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>
我试图使用'codeline.units_alloc',但这不起作用,同样的错误被标记。
任何建议,或指点,以帮助我解决这个问题?
谢谢。
解决方案:由于斯科特小
应用程序/控制器/ contracts_controller.rb
def New
@show_remove = false
....
....
end
应用程序/视图/合同/ _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
这做了它......删除按钮只显示在第二行和后续的属性。
感谢James Scott Jr提供的解决方案: – thomasvermaak 2013-02-21 23:06:52