Topic: Show image if number < 30 else show altternate image

I am trying to write a method that will show an image if the number of days is greater than 30 else show a different image.

I have:
def num_of_days
  num_of_days = (dor - transfer)/96400.to_i
end

def show_flag
  if num_of_days > 30.0
    puts "green"
  else
    puts "red"
  end
end
being I do not yet know how to put the image in I tried to display the words green and red for the time being. Then in my veiw I did this:
<p>
  <%= @hrreview.show_flag %>
</p>

but nothing shows at all, and but num_of_days does calculate and show the number of days.... so I am confused what I am doing wrong.

Re: Show image if number < 30 else show altternate image

The most simple way to fix this is you need to return the string from the method show_flag and not do a puts.  Continuing to keep things as simple as possible I would put the logic in the show_flag method in a view helper and not the model. http://railscasts.com/episodes/101-refa … per-object

Re: Show image if number < 30 else show altternate image

I am not sure how to acheive this... could you PLEASE try to show me??

Re: Show image if number < 30 else show altternate image

In the model:

def show_flag
  if num_of_days > 30.0
     "green"
  else
     "red"
  end
end

If you implemented it in a helper (app/helpers/application_helper.rb):

module ApplicationHelper
  def show_flag_for hrreview
    if hrreview.num_of_days > 30.0
      image_tag 'green-icon.png'
    else
      image_tag 'red-icon.png'
    end
  end
end

And in the view:

<p>
  <%= show_flag_for @hrreview %>
</p>

And if implemented in a view helper you would not need the show_flag method on the model.