0

I have a bit of code in RhoMobile that shows results of a search. I want to display a message if no results are found but as a Ruby n00b I'm not getting the message I want output.

<ul data-role="listview">
   <% @employees.each do |employee| %>

     <li>
       <a href="<%= url_for :action => :show, :id => employee.object %>">
         <%= employee.name %>
       </a>
     </li>

   <% end %>
   <% "<li>No results found</li>" if @employees.empty? %>
</ul>

How to fix this?

Martlark
  • 14,208
  • 13
  • 83
  • 99

1 Answers1

2

You're missing the =, it should be:

<%= "<li>No results found</li>" if @employees.empty? %>

Though that might not work either because the string isn't marked as HTML safe. That said, it's probably best to wrap everything in a conditional to make it more clear and avoid having HTML in a string:

<ul data-role="listview">
  <% if @employees.any? %>
    <% @employees.each do |employee| %>
      <li>
        <%= link_to employee.name, {:action => :show, :id => employee.object} %>
      </li>
    <% end %>
  <% else %>
    <li>No results found</li>
  <% end %>
</ul>

I've also replaced your hand-coded link with a call to link_to.

Martlark
  • 14,208
  • 13
  • 83
  • 99
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
  • This code worked with the link, your link_to did not get any where on rhoMobile. <%= link_to employee.name, {:action => :show, :id => employee.object} %> – Martlark Apr 17 '12 at 06:00
  • @Martlark Well you tagged your question as Rails, and `link_to` will work in Rails. I really know much about Rhombile, but if it doesn't use Rails underneath than you should retag your question. – Andrew Marshall Apr 17 '12 at 12:13
  • It does use ruby on rails. And modifying the link_to in your code as in my comment makes it work. – Martlark Apr 18 '12 at 00:30
  • Well the way I had it should work, even in Rails 2.3 (and perhaps earlier), unless you're not using RESTful routes for some reason. – Andrew Marshall Apr 18 '12 at 04:37