Problem:
if returned result single result each method fail – how code can be improved?
The case:
controller action:
@results= Person.find_by_name('Ben');
view:
<% if nil!=@results %>
<% @results.each do |r| %>
<h2>courses:</h2> <a href="/course/<%= r.name %>/"><%= r.name %></a>
<% end %>
<% else %>
<h2>no results</h2>
<% end %>
result:
undefined method `each' for #<Person id: 2, name: "Ben">
Thanks
A common idiom is to use
Array()Array()will always return something that’s enumerable, even if it’s nil.Then you can keep the rest of your code the same. But I would suggest cleaning it up a bit using the rails helper
present?