Note that there are some explanatory texts on larger screens.

plurals
  1. POCount an Attribute on a nested form?
    primarykey
    data
    text
    <p>Rails 3.2.12 and Ruby 1.9.3 and Haml</p> <p>I would like to use the count of attribute to control the display of a 'link_to "remove"', but I am having problems with setting up the logic.</p> <p>Following is some code from my form as it is currently:</p> <pre><code> .field = codeline.label :name, "Units Alloc" %br/ = codeline.text_field :units_alloc, :precision =&gt; 6, :scale =&gt; 2, :size =&gt; 10, :class =&gt; "ui-state-default" = codeline.hidden_field :_destroy = link_to "remove", '#', class: "remove_fields" </code></pre> <p>this works well but I have the 'remove' link showing up and I would prefer it to only show if there are two :units_alloc attributes.</p> <p>This is what I tried:</p> <pre><code> .field = codeline.label :name, "Units Alloc" %br/ = codeline.text_field :units_alloc, :precision =&gt; 6, :scale =&gt; 2, :size =&gt; 10, :class =&gt; "ui-state-default" - if :units_alloc.count &gt; 1 = codeline.hidden_field :_destroy = link_to "remove", '#', class: "remove_fields" </code></pre> <p>and here is my error:</p> <pre><code> 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 </code></pre> <p>if I use units_alloc in the argument instead of the symbol, I still get an error:</p> <pre><code> 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 #&lt;#&lt;Class:0xadbde90&gt;:0xa8956e8&gt; </code></pre> <p>I tried to use 'codeline.units_alloc' but this did not work and the same error was flagged.</p> <p>Any suggestions, or pointers to help me resolve this issue?</p> <p>Thanks.</p> <p>Solution: Thanks to James Scott Jr.</p> <p>app/controller/contracts_controller.rb</p> <pre><code> def New @show_remove = false .... .... end </code></pre> <p>app/views/contracts/_codelines_fields.html.haml</p> <pre><code> .field = codeline.label :name, "Units Alloc" %br/ = codeline.text_field :units_alloc, :precision =&gt; 6, :scale =&gt; 2, :size =&gt; 10, :class =&gt; "ui-state-default" - if @show_remove = codeline.hidden_field :_destroy = link_to "remove", '#', class: "remove_fields" - else - @show_remove = true </code></pre> <p>And that did it ... the remove button only shows in the second and subsequent row of attributes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload