1

After creating new entries this code works:

<%= book.title, :class=>"test" %>

But when database empty its throws error is there any way to display if its empty don't show else show?

similar like this:

<% if ... empty
<p>no record found</p>
<% else %>
<%= book.title, :class=>"test" %>
<% end %>

thanks!

1
  • 1
    Another usefull method: .try(:method). exemple: <%= book.try(:title) || 'no book found', :class=>"test" %> Commented Feb 28, 2014 at 18:36

2 Answers 2

3
<% if book.blank? %>
  <p>no record found</p>
<% else %>
  <%= book.title, :class=>"test" %>
<% end %>
Sign up to request clarification or add additional context in comments.

2 Comments

Also useful is flipping this around to avoid double negatives: if (book.present?).
mucho grazzie signore
0

The try method will allow you to work with an object that may be nil without throwing an exception if it actually is nil. If book is nil then try will return nil and the || will consider that falsy and will use the right side ("no record found"). Note that this will simply treat "no record found" as though it was the title. If that's ok you can try something like this:

<%= book.try(:title) || "no record found", :class => "test" %>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.