1

I have an array of items that I want to list, but there is one specific item called 'tip' that I would like to exclude. I was thinking of something like "unless item.name == 'tip'" but aren't sure where to place something like that

<ul>
  <% @order.each do |item| %>
    <li><h2><%= item["quantity"] %> &times; <%= item["name"] %></h2></li>
  <% end %>
</ul>
csakon
  • 581
  • 2
  • 6
  • 23

2 Answers2

1

Put unless .. end inside <% .. %>:

<ul>
  <% @order.each do |item|
       unless item['name'] == 'tip' %>
    <li><h2><%= item["quantity"] %> &times; <%= item["name"] %></h2></li>
  <%   end
     end %>
</ul>

Example (used item['name'] instead of item.name for brevity):

require 'erb'

class Listing
  def build
    @order = [
      {'quantity' => 1, 'name' => 'a'},
      {'quantity' => 2, 'name' => 'tip'},
      {'quantity' => 3, 'name' => 'c'},
    ]

    template = ERB.new <<-TMPL
    <ul>
      <% @order.each do |item|
           unless item['name'] == 'tip'%>
        <li><h2><%= item["quantity"] %> &times; <%= item["name"] %></h2></li>
      <%   end
         end %>
    </ul>
    TMPL
    template.result binding
  end
end

puts Listing.new.build

output:

<ul>

    <li><h2>1 &times; a</h2></li>

    <li><h2>3 &times; c</h2></li>

</ul>
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • When I do that I get the following error: (erb):10: syntax error, unexpected $end, expecting keyword_end ; _erbout.force_encoding(__ENCODING__) ^ – csakon Jan 20 '14 at 06:15
  • @Agis I noticed the double <% end %> and only had one, but I still receive that same error. I'm not quite sure why... – csakon Jan 20 '14 at 06:24
  • 1
    @csakon There must be a syntax error somewhere else in your file. The answer's code is fine. Can you post the whole file? – Agis Jan 20 '14 at 06:25
  • @Agis, Why did you remove `end` (without edit comment) ? – falsetru Jan 20 '14 at 06:27
  • @Agis I still get this error - undefined method `name' for {"quantity"=>1, "item"=>"The Maine Event"}:Hash – csakon Jan 20 '14 at 06:47
  • @csakon, The code in your question contains ` item.name == 'tip' `. Maybe you mean `item['name'] == 'tip'` ? I change the code accordingly. – falsetru Jan 20 '14 at 06:48
  • @csakon, I'm not Agis. You don't need to tag my username when you comment to my answer. – falsetru Jan 20 '14 at 07:05
  • Oops, my bad. Thank you to @falsetru – csakon Jan 21 '14 at 05:11
0

There are plenty of possibilities to achieve a goal. I would suggest to use next keyword since it gives you an ability to forget circumstances in the latter code, plus it does not require an enclosing end keyword (when you have more code lines under the condition, it quickly becomes hard to take a glance on where unless … end clause begins/ends):

<ul>
  <% @order.each do |item|
       next if item["name"] == 'tip' %>
    <li><h2><%= item["quantity"] %> &times; <%= item["name"] %></h2></li>
  <% end %>
</ul>
Community
  • 1
  • 1
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • I receive this error when I try your code : undefined method `name' for {"quantity"=>1, "item"=>"The Maine Event"}:Hash – csakon Jan 20 '14 at 06:20