Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9309339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:50:32+00:00 2026-06-19T00:50:32+00:00

Rails 3.2.12 and Ruby 1.9.3 and Haml I would like to use the count

  • 0

Rails 3.2.12 and Ruby 1.9.3 and Haml

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.

Following is some code from my form as it is currently:

    .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"

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.

This is what I tried:

    .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"

and here is my error:

       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

if I use units_alloc in the argument instead of the symbol, I still get an error:

        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>

I tried to use ‘codeline.units_alloc’ but this did not work and the same error was flagged.

Any suggestions, or pointers to help me resolve this issue?

Thanks.

Solution: Thanks to James Scott Jr.

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

And that did it … the remove button only shows in the second and subsequent row of attributes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:50:33+00:00Added an answer on June 19, 2026 at 12:50 am

    By the time you’re in the form (partial), codeline doesn’t refer to an instance the instance of Codeline that the form (partial) is for, but an instance of an ActionView::Helpers::FormBuilder that simple knows how to associate information the the instance of Codeline. You knew that because in the first line of the partial, you have codeline.object.build_code.

    So, if you want to access the information about the units_alloc associated, you would access them with codeline.object.units_alloc. That will give you your data for your conditional.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Haml in a Ruby on Rails project. I know you use
I'm writing a non-Rails ruby application (gasp!) and would like to be able to
In my Ruby on Rails Project I use HAML, I need to translate something
Please forgive me. I am definitely a beginner at Haml and Ruby on Rails.
We're using Rails 3.2.3, Ruby 1.9.1, and HAML. We have a Post model and
I'm a ruby/haml/sass-beginner. I just installed ruby and rails for windows, haml, and sass
If using HAML on Ruby on Rails, then :sass #someDiv border: 3px dashed orange
I am creating a system that stores cards using Ruby/Rails/HAML - In this case
I am having a ruby on rails application.I added a new section called reports
I'm very new to Ruby on Rails, and even newer to Haml, i started

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.