edgarjs / ajaxful-rating

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

Rating not updated without page refresh #34

Closed ghost closed 13 years ago

ghost commented 14 years ago

Hi,

At present in my app the rating is not being updated after clicking on the stars, although the value is being recorded as after a page refresh the new rating appears. In my view:

Average: <%= ratings_for @place, :static %></br>
Your Rating: <%= ratings_for @place, @user, :show_user_rating => true %>

Which generates:

Average: <div class="ajaxful-rating-wrapper" id="ajaxful_rating_no-small_place_2"><ul      class="ajaxful-rating"><li class="show-value" style="width: 50.0%">Global rating average: 2.5 out of 5</li><li><span class="stars-1" title="Global rating average: 2.5 out of 5">2.5</span></li><li><span class="stars-2" title="Global rating average: 2.5 out of 5">2.5</span></li><li><span class="stars-3" title="Global rating average: 2.5 out of 5">2.5</span></li><li><span class="stars-4" title="Global rating average: 2.5 out of 5">2.5</span></li><li><span class="stars-5" title="Global rating average: 2.5 out of 5">2.5</span></li></ul></div></br>

Your Rating:

  • Your rating: 1 out of 5
  • 1
  • 2
  • 3
  • 4
  • 5

I'm using Ruby 1.9.1 and Rails 3.0.0beta3 with jQuery, any help would be much appreciated! Awesome plugin btw...

ghost commented 14 years ago

Sorry, I'll try that again...

Average: <div class="ajaxful-rating-wrapper" id="ajaxful_rating_no-small_place_2"><ul class="ajaxful-rating"><li class="show-value" style="width: 50.0%">Global rating average: 2.5 out of 5</li><li><span class="stars-1" title="Global rating average: 2.5 out of 5">2.5</span></li><li><span class="stars-2" title="Global rating average: 2.5 out of 5">2.5</span></li><li><span class="stars-3" title="Global rating average: 2.5 out of 5">2.5</span></li><li><span class="stars-4" title="Global rating average: 2.5 out of 5">2.5</span></li><li><span class="stars-5" title="Global rating average: 2.5 out of  5">2.5</span></li></ul></div></br>
Your Rating: <div class="ajaxful-rating-wrapper" id="ajaxful_rating_no-small_place_2"><ul class="ajaxful-rating"><li class="show-value" style="width: 20.0%">Your rating: 1 out of 5</li><li><a href="/places/2/rate?dimension=&amp;show_user_rating=true&amp;small=false&amp;stars=1" class="stars-1" data-method="post" data-remote="true" rel="nofollow" title="Rate 1 out of 5">1</a></li><li><a href="/places/2/rate?dimension=&amp;show_user_rating=true&amp;small=false&amp;stars=2" class="stars-2" data-method="post" data-remote="true" rel="nofollow" title="Rate 2 out of 5">2</a></li><li><a href="/places/2/rate?dimension=&amp;show_user_rating=true&amp;small=false&amp;stars=3" class="stars-3" data-method="post" data-remote="true" rel="nofollow" title="Rate 3 out of 5">3</a></li><li><a href="/places/2/rate?dimension=&amp;show_user_rating=true&amp;small=false&amp;stars=4" class="stars-4" data-method="post" data-remote="true" rel="nofollow" title="Rate 4 out of 5">4</a></li><li><a href="/places/2/rate?dimension=&amp;show_user_rating=true&amp;small=false&amp;stars=5" class="stars-5" data-method="post" data-remote="true" rel="nofollow" title="Rate 5 out of 5">5</a></li></ul></div>
edgarjs commented 14 years ago

Do you update the view in your controller?

ghost commented 14 years ago

Hi, thanks for the prompt response - I've got the rate action in my place controller (and routes) taken directly from the wiki, I can post it when I get home.

ghost commented 14 years ago

Sorry about the delay. In my places controllers:

def rate
  @place = Place.find(params[:id])
  @user = User.find(current_user.id)

  @place.rate(params[:stars], @user, params[:dimension])

  render :update do |page|
    page.replace_html @place.wrapper_dom_id(params), ratings_for(@place, params.merge(:wrap => false))
    page.visual_effect :highlight, @place.wrapper_dom_id(params)
  end
end

In my routes.rb file:

resources :places do
  resources :comments

  member do
    post :rate
  end
end

The stars are fine, and they highlight on mouse-over, but it seems the ajax bit isn't quite working...thanks for your help!

edgarjs commented 14 years ago

Humm, try logging the value of @place.wrapper_dom_id(params) and check if it's exactly the same as in your markup. There was a problem with this method on previous versions, where it wasn't generating the correct dom id.

ghost commented 14 years ago

Using <%= debug @place.wrapper_dom_id(params) %> gives --- ajaxful_rating_place_2

I've just noticed a no method error after clicking on a star though:

NoMethodError (undefined method `encoding' for []:Array):
  app/controllers/places_controller.rb:37:in `block in rate'
  app/controllers/places_controller.rb:36:in `rate'

Line 36 is render :update..., and 37 is page.replace_html...

edgarjs commented 14 years ago

Can you paste the full trace please?

ghost commented 14 years ago

After clicking, I get:

Started POST "/places/2/rate?   dimension=&show_user_rating=true&small=false&stars=4" for 127.0.0.1 at 2010-06-22 16:11:59
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
  Processing by PlacesController#rate as JS
  Parameters: {"dimension"=>"", "show_user_rating"=>"true", "small"=>"false", "stars"=>"4", "id"=>"2"}

 [1mPlace Load (0.3ms) SELECT "places".* FROM "places" WHERE ("places"."id" = 2) LIMIT 1  [1mCACHE (0.0ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1  [1mRate Load (0.3ms) SELECT "rates".* FROM "rates" WHERE ("rates".rateable_id = 2 AND "rates".rateable_type = 'Place' AND ("rates"."dimension" IS NULL)) AND ("rates"."rater_id" = 1) LIMIT 1 CACHE (0.0ms) SELECT "rates".* FROM "rates" WHERE ("rates".rateable_id = 2 AND "rates".rateable_type = 'Place' AND ("rates"."dimension" IS NULL)) AND ("rates"."rater_id" = 1) LIMIT 1  [1mSQL (0.4ms) UPDATE "rates" SET "stars" = 4, "updated_at" = '2010-06-22 15:11:59.645047' WHERE ("rates"."id" = 1)  [1mSQL (0.3ms) SELECT SUM("rates"."stars") AS sum_id FROM "rates" WHERE ("rates".rateable_id = 2 AND "rates".rateable_type = 'Place' AND ("rates"."dimension" IS NULL)) SQL (0.2ms) SELECT COUNT(*) AS count_id FROM "rates" WHERE ("rates".rateable_id = 2 AND "rates".rateable_type = 'Place' AND ("rates"."dimension" IS NULL))  [1mSQL (0.4ms) UPDATE "places" SET "rating_average" = 4.0, "updated_at" = '2010-06-22 15:11:59.653100' WHERE ("places"."id" = 2)

NoMethodError (undefined method `to_hash' for "4":String):
  app/controllers/places_controller.rb:37:in `block in rate'
  app/controllers/places_controller.rb:36:in `rate'

Rendered /Users/soniabracegirdle/.rvm/gems/ruby-1.9.1-p378/gems/actionpack-3.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)

 [1mSQL (1.0ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence'  [1mSQL (0.3ms)  SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence'  SQL (0.4ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence'  [1mSQL (0.4ms)  SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence'   [1mSQL (0.4ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence'  [1mSQL (0.4ms)  SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence'  SQL (0.3ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence'  [1mSQL (0.4ms)  SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence'  Rendered /Users/soniabracegirdle/.rvm/gems/ruby-1.9.1-p378/gems/actionpack-3.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (242.8ms) Rendered /Users/soniabracegirdle/.rvm/gems/ruby-1.9.1-p378/gems/actionpack-3.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (317.5ms)

There's also a google map on the page, which displays the location of the places...don't know if that's related!

ghost commented 14 years ago

Just noticed the no method error has changed this time round...on looking at the log it seems it's almost always 'encoding' that is undefined.

edgarjs commented 14 years ago

Does it throw anything else after the app/controllers/places_controller.rb:36:in rate ? I need to see the full trace, inside the rate method. I don't know why it's trying to do a to_hash to the 4...

ghost commented 14 years ago

I've pasted my entire places controller up: http://pastie.org/1016826, along with the log (from loading places#show, then updating a rating for a signed-in user): http://pastie.org/1016830. Is there anything else I can do to debug? I'm quite new to RoR, sorry for not being more helpful!

edgarjs commented 14 years ago

I just noticed you're using ruby 1.9.1. Is it possible to try it with 1.8.7? It seems like it has something there.

ghost commented 14 years ago

thanks for all the help, unfortunately I've just managed to overwrite my entire app - I'm overseas at the moment so a bit stuck for now! When I get it back up and running again I'll have a go with 1.8.7...

ghost commented 14 years ago

Just to confirm, the code above works fine running rails 3 beta 4 with 1.8.7. I had some issues using jQuery as well, so have switched back to prototype. Cheers!

edgarjs commented 14 years ago

Ok, that's great. Thanks for the feedback. I'll look forward to support more ruby versions in the future.

2case commented 14 years ago

i run also in this 'encoding' error. i fixed it with calling the rating_for helper from a partial instead direct calling inside my controller:

page.replace_html :info_ratings_overall, :partial => 'static' page.replace_html @game.wrapper_dom_id(params), :partial => 'interactive'

looks very strange... (macosx, ruby 1.9.2, rails3-beta4, ajaxful_rating 2.2.8.1)

RichardJordan commented 14 years ago

I get this same problem and am trying to follow the fixes for it but cannot - using ruby 1.8.7 with rails 2.3.8... it is saving the rating but not updating the page without a refresh

marcoam commented 13 years ago

I'm getting a similar problem, it gives me the following error: RJS error:

TypeError: Element.update is not a function

jhender commented 13 years ago

Same as Marcoam I am getting the RJS error and require refresh to show the new rating. Any help?

Error Popup 1:

RJS error:

TypeError: Object function Element() { [native code] } has no method 'update'

Error Popup 2:

Element.update("ajaxful_rating_no-small_item_3", "<ul class=\"ajaxful-rating\"><li class=\"show-value\" style=\"width:     60.0%\">Global rating average: 3.0 out of 5</li><li><a href=\"/items/canon-eos-450d/rate?dimension=&amp;show_user_rating=false&amp;small=false&amp;stars=1\" class=\"stars-1\" data-method=\"post\" data-remote=\"true\" rel=\"nofollow\" title=\"Rate 1 out of 5\">1</a></li><li><a href=\"/items/canon-eos-450d/rate?dimension=&amp;show_user_rating=false&amp;small=false&amp;stars=2\" class=\"stars-2\" data-method=\"post\" data-remote=\"true\" rel=\"nofollow\" title=\"Rate 2 out of 5\">2</a></li><li><a href=\"/items/canon-eos-450d/rate?dimension=&amp;show_user_rating=false&amp;small=false&amp;stars=3\" class=\"stars-3\" data-method=\"post\" data-remote=\"true\" rel=\"nofollow\" title=\"Rate 3 out of 5\">3</a></li><li><a href=\"/items/canon-eos-450d/rate?dimension=&amp;show_user_rating=false&amp;small=false&amp;stars=4\" class=\"stars-4\" data-method=\"post\" data-remote=\"true\" rel=\"nofollow\" title=\"Rate 4 out of 5\">4</a></li><li><a href=\"/items/canon-eos-450d/rate?dimension=&amp;show_user_rating=false&amp;small=false&amp;stars=5\" class=\"stars-5\" data-method=\"post\" data-remote=\"true\" rel=\"nofollow\" title=\"Rate 5 out of 5\">5</a></li></ul>");
new Effect.Highlight("ajaxful_rating_no-small_item_3",{});
edgarjs commented 13 years ago

Hey jhender, are you using rails 3?

jhender commented 13 years ago

yes i am

edgarjs commented 13 years ago

the correct version for rails 3 is beta.3 However as its name says it's still in beta and not fully supported, but you error is related to the new rails 3 API, not a gem's error. In rails 3 there's no update in the controller for ajax anymore.