Question

I wonder what's the simplest way to implement stars review in Rails

I tried to use the following snippet,

$('.rating span').click(function(){
    alert($(this).attr('id'));
})

however the problem is I don't know how to finish it, for example I didn't get how to fix the colored stars and where to save a value, more complicated when rendering review how to color stars based on review integer I have, preferably how to color partial star when I have non-integer review value.

If there are good gem or simple steps how to proceed with above snippet I would appreciate for a help.

Was it helpful?

Solution

If you are willing to go for non-integer values as well. I will suggest you to use the plugin raty. Its a very simple plugin and can be used to highlight based on a number.

for your problem to save in the database. You can send an ajax and get the field updated.

var score = save the current score;

$.ajax({
  type: 'post',
   url : 'url/you/want'
  data: { rating : score, id: id}
  success : function(){
    //do what you want
  }
})

according to the above ajax, the value will be posted to the action where the url points to and will be posted there.

Inside that method you can receive the parameter to save it.

def methodname
  if params[:rating]
    @movie = Movie.find(params[:id])
    @movie.update_attributes(:rating => params[:rating])
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top