How to combine embedded ruby with a ternary operator in a view

Currently I have in my view something like this:

<table>
<tr>
   <% if item.status == nil %>
       <td><%= image_tag "/assets/nil.gif" %></td>
   <% else %>
       <% if item.status == "1" %>
           <td><%= image_tag "/assets/yes.gif" %></td>
       <% else %>
           <td><%= image_tag "/assets/no.gif" %></td>
       <% end %>
   <% end %>
</tr>
...

Can I use a ternary operator here? I didn't know where to put the ? or the : when using this combination of embedded ruby and html.

Answers


<%= 1 == 1 ? "one is one" : "one is two" %>
# outputs "one is one"

Therefore:

<%= image_tag "/assests/#{ item.status == "1" ? "yes" : "no"}.gif" %>

However, in this case, since you are testing three possible values in all, a switch statement inside a helper method might be best.

# app/helpers/items_help.rb

def gif_name(status)
  case status
  when nil
    "nil"
  when "1"
    "yes"
  else
    "no"
  end
end

# app/views/items/action.html.erb

<td><%= image_tag "/assests/#{gif_name(item.status)}.gif" %></td>

Need Your Help

SQL - Foreign key constraints and cardinality

sql sqlite cardinality

Can someone explain foreign key constraints and cardinality? I am reading the SQLite manual and I am at the section on advanced foreign key constraint features. In its explanation it says:

How to add records to database using a joined table - codeigniter

php mysql forms codeigniter join

I made a form for adding factories to my database. this works great but i can't show my factories on the page because they're in a joined table, but when i submit the page it does not add an id to ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.