edgarjs / ajaxful-rating

Provides a simple way to add rating functionality to your application.
http://rdoc.info/projects/edgarjs/ajaxful-rating
418 stars 127 forks source link

Rails 3.1 #56

Open jspooner opened 13 years ago

jspooner commented 13 years ago

Anyone tried to use this with rails 3.1? I think the ajax helpers have been removed so none of the javascript works. I'd be happy to fork and update if your not working on that.

jspooner commented 13 years ago

Sorry I was wrong about this. I think there is a styling error so the links are not displayed

edgarjs commented 13 years ago

There is a branch that was started for rails 3.x It works fine on rails 3.0.x but haven't tried on 3.1. The branch is rails3

jspooner commented 13 years ago

RJS has been extracted from 3.1 and placed in the prototype-rails gem.

http://weblog.rubyonrails.org/2011/4/21/jquery-new-default

jspooner commented 13 years ago

Here is how I integrated with my 3.1 rc5.

your controller

  # POST
  def rate
    @location = Location.find(params[:id])
    respond_to do |format|
      if @location.rate(params[:stars], current_user, params[:dimension])
        format.js { render :partial => "rating" }
      else
        format.js { render :partial => "rating" }
      end
    end
  end

_rating.html.erb

<%= ratings_for @location, current_user %>

app.js

I'm using jquery-ujs to handle the response. This lib is included with the 'jquery-rails'gem. https://github.com/rails/jquery-ujs

$('a').live('ajax:complete', function(xhr, status) {
  $(".ajaxful-rating-wrapper").replaceWith(status.responseText)
});

ajaxful_ratings.css

Not sure if this is by design or not but I had to set a width on the wrapper so only 5 stars were displayed. It showed all by default.

#sidebar .ajaxful-rating-wrapper {width:125px;}

show.html.erb

<%= render "rating" %>
jspooner commented 13 years ago

Also with 3.1 you don't need to copy the assets into the public directory.

Ryan did a nice demo explaining how to use the different asset directories. http://railscasts.com/episodes/279-understanding-the-asset-pipeline

I could probably find the time to make a 3.1 branch if you want.

edgarjs commented 13 years ago

The only rjs helper that I'm "using" is in the README.md file for demonstration only of how you'd like to use it. But that's not dependent of the gem, you can use any js method to update the wrapper. The gem itself doesn't use any rjs helper.

josephers commented 12 years ago

This was incredibly useful for me. Can we leave this open or move this to the documentation?

jspooner commented 12 years ago

@josephers +1

jimishjoban commented 12 years ago

Thanks a lot jspooner for putting up that code snipet.

For others, If you are allowing users to rate on multiple dimension, then you must replace the correct wrapper. I did something like this:

   $('a').live('ajax:complete', function(xhr, status) {
      $(this).parent().parent().parent().replaceWith(status.responseText)
   });

And my _rating.html.erb looks like this:

  <%= ratings_for @merchant, current_user, :dimension => dimension, :show_user_rating => true %>

And I pass dimension from merchant controller:

respond_to do |format|
  format.js { render :partial => "rating", :locals => {:dimension => params[:dimension]} }
end

Hope that helps someone.

PikachuEXE commented 12 years ago

@jimishjoban That certainly helps me! I hate js.erb... Is there a cleaner way to do updating like this? (I mean...create,update,delete items...)

zben commented 12 years ago

this question might be silly. when you include gem 'ajaxful-rating', how do you specify it to use Rails3 branch? I guess I can go to the gem folder and do git checkout. But if i am deploying on heroku how do i specify it in GemFile? Thanks.

edgarjs commented 12 years ago

You just need to add the beta version, like:

gem 'ajaxful_rating', "~> 3.0.0.beta8"

edgar.js

On Sat, Feb 4, 2012 at 11:01 AM, Ben Zhang < reply@reply.github.com

wrote:

this question might be silly. when you include gem 'ajaxful-rating', how do you specify it to use Rails3 branch? I guess I can go to the gem folder and do git checkout. But if i am deploying on heroku how do i specify it in GemFile? Thanks.


Reply to this email directly or view it on GitHub: https://github.com/edgarjs/ajaxful-rating/issues/56#issuecomment-3811435

mauroc commented 12 years ago

I too have had issues after upgrading to Rails 3.1, even after reverting back to prototype. The rating stars render OK on the page, but the controller stops for a no-method error upon calling the ratings_for helper (the rates table is updated correctly in the previous statement). Note that the helper executes correctly when called by the .erb view. I have tried to follow jspooner's solution but have run aground on the java snippet. I guess I don't know where to place the snippet and how to invoke it, being pretty green on javascript. Any help is truly appreciated!

katafrakt commented 12 years ago

Have you tried a solution from wiki? It worked for me recently.

mauroc commented 12 years ago

I used that solution and it works great. Next time I will remember to check the wiki :-). Thank you katafrakt!

pslaski commented 12 years ago

I tried jspooner and wiki solutions but it not enough. (I use rails 3.1.4 and Devise) When I want to rate my place I get: 'WARNING: Can't verify CSRF token authenticity' and my user session is dump + rating didn't happen. Are anybody use ajaxful_rating with rails 3.1.4 and Devise?

greendog99 commented 12 years ago

I'm using ajaxful_rating with rails 3.1.3 and Devise (working now thanks to the wiki docs), haven't had any CSRF problems. I assume when you "view source" on your page, you see the CSRF meta tag (like this):

<meta content="onlk5iB/F5yATptrFg7iMHwq9iU5tim8+kc0SVmYFY=" name="csrf-token" />

I'm planning to upgrade to Rails 3.2.3 this week and see if anything breaks.

pslaski commented 12 years ago

Yes, I have CSRF meta tag but I don't have token in request's parameters (It should be send by default but it doesn't)