Forum: Ruby on Rails helper + conditional loop

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Anant S. (Guest)
on 2007-06-15 17:49
hi everyone ,

   I am new to RoR thing , i am building an application in that i am
filling the text field with items present in my database ...thus using
code like this :

     <% if @lost_pro_d.disease != "" %>
    Disease: <%= @lost_pro_d.disease %><br />
     <% end %>
     <% if @lost_pro_d.noticable_mark != ""%>
    Noticeable Marks: <%= @lost_pro_d.noticable_mark%><br />
     <% end %>
     <% if @lost_pro_d.habits != ""%>
    Habits: <%= @lost_pro_d.habits%>
     <% end %>

   what am i trying is to use helper thing for this ....can anyone guide
me how to do this thing ????

Anant
Steve R. (Guest)
on 2007-06-15 19:48
Anant S. wrote:
> hi everyone ,
>
>    I am new to RoR thing , i am building an application in that i am
> filling the text field with items present in my database ...thus using
> code like this :
>
>      <% if @lost_pro_d.disease != "" %>
>     Disease: <%= @lost_pro_d.disease %><br />
>      <% end %>
>      <% if @lost_pro_d.noticable_mark != ""%>
>     Noticeable Marks: <%= @lost_pro_d.noticable_mark%><br />
>      <% end %>
>      <% if @lost_pro_d.habits != ""%>
>     Habits: <%= @lost_pro_d.habits%>
>      <% end %>
>
>    what am i trying is to use helper thing for this ....can anyone guide
> me how to do this thing ????
>
> Anant

In your helper:

def display_if_present(desc, item)
  return "" if item.blank?
  return "#{desc}: #{item}<br/>"
end

In the view:

<%= display_if_present('Disease', @lost_pro_d.disease %>

steve
This topic is locked and can not be replied to.