Rails3.2.12およびRuby1.9.3およびHaml
count属性を使用して'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"
これはうまく機能しますが、「remove」リンクが表示されているので、:units_alloc属性が2つある場合にのみ表示することをお勧めします。
これは私が試したものです:
.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'を使用しようとしましたが、これは機能せず、同じエラーが報告されました。
この問題を解決するのに役立つ提案やポインタはありますか?
ありがとう。
解決策:JamesScottJrに感謝します。
app / controller / Contracts_controller.rb
def New
@show_remove = false
....
....
end
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
そしてそれはそれをしました...削除ボタンは属性の2番目以降の行にのみ表示されます。