8bithero
8bithero

Reputation: 1609

Ruby on Rails Loops

I want to create the following loop in rails:

If value<=5 
display image1.gif value.times && display image2.gif (5-value).times

I was trying to add this to my View, but is there a way I can convert it into a method? How would I put the image tags if I where to make it a helper method?

Thanks in advance for any help.

Upvotes: 0

Views: 720

Answers (3)

8bithero
8bithero

Reputation: 1609

(For star rating system) Got it working like this:

def display_stars content_tag :span, :class => 'stars' do
  current_admin_rating.times do 
    concat(image_tag("image1.gif";, :size => "30x30", :class => "gold")) 
  end 

  (5-current_admin_rating).times do 
    concat(image_tag("image2.gif";, :size => "30x30", :class => "gold")) 
  end 
  nil 
end 

Upvotes: 0

tokland
tokland

Reputation: 67900

def show_starts(value)
  if value <= 5 
    image_tag("image1.gif")*value + image_tag("image2.gif")*(5-value)
  end
end

Upvotes: 1

aNoble
aNoble

Reputation: 7072

This should work if I'm understanding what you're looking for.

Put this in a helper file:

def image_helper(value, image1, image2)
  html = ''

  if value <= 5
    value.times { html += image_tag(image) }
    (5 - value).times { html += image_tag(image2) }
  end

  html.html_safe
end

Then call it from your view like like:

image_helper(value, 'image1.gif', 'image2.gif')

Upvotes: 0

Related Questions