user661684
user661684

Reputation: 221

Passing score variable to jquery raty

I am using jquery raty to implement star rating and score display for my ruby on rails site. i can use the plugin to accept votes however i am stuck to display the scores after they are retrieved from the DB. Say i need to show a vote from the DB, the voting user and the score are stored in a variable say @user.score (e.g. @user.score = 3).

  1. What is the syntax to display the stars in the .erb form? I know if the score is a constant, e.g. 3, I can write something like:

    $('#star').raty({
readOnly: true,
start: 3
});

<div><div id="star"></div>

OR

$('#start').raty({ start: function() { return $(this).attr('data-rating'); } }); <div id="star" data-rating="3"></div>

But now the score value 3 is stored in @user.score (a variable). How can I pass the score value to the star display?

  1. Public function $('#star').raty('score'); looks like it is doing something to the score but I don't understand what it is doing. Can tell me?

  2. Maybe I am in a wrong direction to display the retrieved score. Please tell me.

Please help Thanks batterhead

Upvotes: 1

Views: 2927

Answers (2)

jedison
jedison

Reputation: 976

The correct answer would appear to be:

$('#star').raty({ score: 2 });

so, you can do

$('#star').raty({ score: myScore });

Upvotes: 1

jfriend00
jfriend00

Reputation: 707696

Here's the doc page for the .raty() plugin. It looks to me like there are several examples for how to initialize a given set of ratings with it's initial value. Here's one of those examples:

$('#star').raty({
  start: 2
});

<div id="star"></div>

If you have the score in a variable named myScore, you can do this:

$('#star').raty({
  start: myScore
});

Upvotes: 0

Related Questions