Search code examples
ruby-on-railsrubyhaml

Ruby: .each iteration not working correctly


I have this assignment:

links: { :default => { :some_key => 'some value' }, :not_default => { :another_key => 'another value' } }

Now with the following iteration the div .divider gets rendered even if the value it contains equals to 'default':

- links.each do |key, value|
    - if key != 'default'
        .divider= key

It results in:

<div class="divider">default</div>
<div class="divider">not_default</div>

That behavior cannot be intended.


Solution

  • The key you have is the Symbol :default, not a String, which is why your if always evaluates to true. You can fix this by doing key != :default.

    - links.each do |key, value|
      - if key != :default
        .divider= key