November 11, 2008

acts_as_rateable issues

If you’re trying to use “acts_as_rateable” bare in mind the tutorials I have found are not yet updated and posing some problems.

The first one I ran into was the migration generated in the tutorial.  The tutorials I have found have you script/generate a migration which you add code to generate the ratings table.  You need a rates join table as well, which isn’t included and will cause the obvious table “ratings” not found error.

I noticed “acts_as_rateable” has a migration task built in so  instead of generating your own migration(s) just do:

./script/generate acts_as_rateable_migration

and everything will be A-OK

My rating controller, which only contains a rate action:

I removed the user stuff, im doing anon voting and dont want my users voting

I changed:

rateable.add_rating Rating.new(:rating => params[:rating], :user_id => @current_user.id)

TO

rateable.rate_it params[:rating], params[:session_id]

If you’re doing rate per user then params[:session_id] would be your current_user.id or whatever

Next for the partial referenced in the tutorial I changed the link_to_remote call to read:

<%= link_to_remote( "#{x}", 
:url => { :controller => "rating", :action =>rate",
:id => asset.id, :rating => x,:rateable_type => asset.class.to_s},
:html => { :class => "#{@convert[x-1]}-stars",
:name => "#{pluralize(x,'star')} out of 5",:title => "Rate this a #{x} out of 5" } ) %>

Next I added a route to routes.rb (I just need the /rate for my app - nothing more)


map.rate '/rate', :controller => 'rating', :action => 'rate'

Taking the original tutorial, applying these few changes, rendered me a slick 5-star rating system for my models.

Comments (View)
blog comments powered by Disqus