Forum: Ruby on Rails Star Rating Component?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Martin Smith (Guest)
on 2006-03-07 15:42
Hi,

I'm looking for a star rating component for RoR, a bit like Votio
(http://redalt.com/downloads/ - find the votio heading) or the star
rating used on Amazon.

I don't really need the AJAX capabilities, just the ability to bind the
results to a hidden drop down, or radio inputs.

Multiple raters per page is also an issue.

Any recomendations?
Peter De Berdt (Guest)
on 2006-03-07 16:01
(Received via mailing list)
On 07 Mar 2006, at 14:42, Martin Smith wrote:

> Multiple raters per page is also an issue.
>
> Any recommendations?

If you have the Rails Recipes book, you could read up on the
JavaScript helper section and implement the open source version from
the Yahoo UI Library and hide it behind one simple ruby command.

Best regards

Peter De Berdt
Steve O. (Guest)
on 2006-03-07 16:28
(Received via mailing list)
Hi Martin,

I've got a flash-version I built and included via flash-javascript
integration kit. Works great. Let me know if you want it and I'll send
it
over.

Steve
Martin Smith (Guest)
on 2006-03-07 17:01
Steve O. wrote:
> Hi Martin,
>
> I've got a flash-version I built and included via flash-javascript
> integration kit. Works great. Let me know if you want it and I'll send
> it
> over.
>
> Steve

Thanks, for that.  I would really prefer to keep it just javascript
though.  I assume that for your version you need flash installed.
Martin Smith (Guest)
on 2006-03-07 17:03
Peter De Berdt wrote:

>
> If you have the Rails Recipes book, you could read up on the
> JavaScript helper section and implement the open source version from
> the Yahoo UI Library and hide it behind one simple ruby command.

Sadly I don't.  I have looked at the Yahoo UI Library, but could only
find the Star Rater in the patterns section, not in the download.

Martin
Peter De Berdt (Guest)
on 2006-03-07 19:07
(Received via mailing list)
On 07 Mar 2006, at 16:03, Martin Smith wrote:

> Peter De Berdt wrote:
>
>>
>> If you have the Rails Recipes book, you could read up on the
>> JavaScript helper section and implement the open source version from
>> the Yahoo UI Library and hide it behind one simple ruby command.
>
> Sadly I don't.  I have looked at the Yahoo UI Library, but could only
> find the Star Rater in the patterns section, not in the download.

Well, the nice thing about javascript is that you can extract it from
a page:

http://us.js2.yimg.com/us.yimg.com/lib/ls/js/
yg_multirate_200602281800.js

They then use the following code to show the rating system:
	<script>
	if (document.getElementById)
	 {
		// Support for Class A.
		rating = new yg_Ratings("rating", "", 0, "star");
	 }
	 else
	 {
		// Support for poor DOM (low grade).
		document.write('<div>');
		document.write('1 Star<input type="radio" name="rating" value="" />');
		document.write('<input type="radio" name="rating1" value="" />');
		document.write('<input type="radio" name="rating2" value="" />');
		document.write('<input type="radio" name="rating3" value="" />');
		document.write('<input type="radio" name="rating4" value="" />5
Stars');
		document.write('</div>');
		//document.write('<input class="save" type="submit" name="submit"
value="Save" class="ylsfbtn" />');
	 }
         </script>

Just go to http://local.yahoo.com/, search for "Restaurants" in
Location "New York", click on a restaurant for the detail (which has
the widget) and then look at the source.


Best regards

Peter De Berdt
Dylan S. (Guest)
on 2006-03-07 19:59
(Received via mailing list)
I think the CSS approach to this is the best.  I implemented the
following
approach, which is very popular:
http://komodomedia.com/blog/index.php/2006/01/09/c...

The code I used below enabled the "ajaxy" part of this... so when you
click
on a star, it automatically saves your pick... much like the Netflix
star
system.

===========================
<ul class='star-rating'>
          <div id='super'>
            <% if @review %>
              <li class='current-rating'
                  style='width: <%= @review.rating * 30 %>px;'>&nbsp;
</li>
            <% end %>
        </div>

            <% for i in 1..5 %>
            <li>
                <%= link_to_remote i, {:update => 'super',
                                       :url   => {:action    =>
'rate_cause',
                                                  :cause_id  =>
@cause.id,
                                                  :user_id   =>
@user.id,
                                                  :rating    => i}},
                                      {:class  =>
num2english(i)+"-stars"}
%>
            </li>
            <% end %>
</ul>
===========================

It works beautifully !
Let me know if you have any questions.
John H. (Guest)
on 2006-03-08 00:19
(Received via mailing list)
Dylan,  Can we see the controller code you are using?

John
Dylan S. (Guest)
on 2006-03-08 18:02
(Received via mailing list)
Sure !  Here you go... but please excuse the mess :)


controller
===============
def rate_cause
id, id, rating = params[:id], params[:id], params[:rating]
@review = Review.find(:first, :conditions => ["id = ? and id = ?", id,
id])
if @review
  @review.update_attribute("rating", rating)
else
  @review = Review.create(:id => id, :id => id, :rating => rating)
end
render(:layout => false)
end
===============


rate_cause.rhtml
===============
<% if @review %>
  <li class='current-rating' style='width: <%= @review.rating * 30
%>px;'>&nbsp;</li>
<% end %>
===============
John H. (Guest)
on 2006-07-17 23:04
(Received via mailing list)
Dylan,

what does your Review table look like?(database schema)

John
Nagesh S. (Guest)
on 2007-08-16 10:57
hi
can uany one give me the entire coding for starrating.
plz............
thanks
This topic is locked and can not be replied to.