views:

36

answers:

1

Hi,

I'm in the process of integrating the jQuery star rating plugin with Rails. I've written the Rails server-side code and have sorted the submission of a new rating, but at the moment I'm struggling to figure out how to display the average rating value as a static series of stars using the plugin. Displaying the rating itself is fine:

<p>
  <b>Rating:</b>
  <%= @place.rating_av %>
</p>

...but what is the best way of passing that value to the stars? Static stars are displayed using the following code (from the plugin site):

<input name="star3" type="radio" class="star" disabled="disabled"/>
<input name="star3" type="radio" class="star" disabled="disabled"/>
<input name="star3" type="radio" class="star" disabled="disabled" checked="checked"/>
<input name="star3" type="radio" class="star" disabled="disabled"/>
<input name="star3" type="radio" class="star" disabled="disabled"/>

This example displays 3 out of 5 stars. I don't need any ajax functionality here, just a simple static display. Should I think about a helper method (eg. if @place.rating_av == 3, output is as shown above)? Any help would be much appreciated!

A: 

I've written a couple of helpers to give the output - for future reference:

def stars(place)
  (place.rating_av.to_f).round
end

def rating_display(place)
  if stars(place) == 0
  render "no_stars"
  elsif stars(place) == 1
  render "one_star"
  elsif stars(place) == 2
  render "two_stars"
  elsif stars(place) == 3
  render "three_stars"
  elsif stars(place) == 4
  render "four_stars"
  else stars(place) == 5
  render "five_stars"
end
end

Then in each partial:

#_three_stars.html.erb
<input name="star3" type="radio" class="star" disabled="disabled"/>
<input name="star3" type="radio" class="star" disabled="disabled"/>
<input name="star3" type="radio" class="star" disabled="disabled" checked="checked"/>
<input name="star3" type="radio" class="star" disabled="disabled"/>
<input name="star3" type="radio" class="star" disabled="disabled"/>

And in the view:

#show.html.erb
<%= rating_display(@place) %>

Still needs a bit of tidying up, but it works fine! If anyone has a better solution, please let me know!

Sonia