Combining html and ruby -


i have html code in rails application looks this:

<td><%= myobj.find_by_id(@my_obj.some_id).name %></td> 

and shows fine. however, field some_id not present in database, i'd display na rather myobj's name in cases.

so here's mean in pseudocode:

<td><%= if @my_obj.some_id nil "na" else myobj.find_by_id(@my_obj.some_id).name %></td> 

how can this?

you can use following snippet:

<td><%= @my_obj.some_id.present? ? myobj.find(@my_obj.some_id).name : 'na' %></td> 

however, you're exposing pretty big issues if you're finding records in view code this. controller should handle kind of logic.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -