ajaxful-rating icon indicating copy to clipboard operation
ajaxful-rating copied to clipboard

Rating possible but no page update

Open helmerj opened this issue 14 years ago • 6 comments

Hi,

I have implemented the ajaxful-rating and I am able to rate my model. BUT I don’t get any page update. in order to see the applied rating I have to reload the page.

Admittedly, I am using the ajaxful-rating gem under rather complicated circumstances:

  1. I rate users, so the the model that is rating is ratable at the same time
  2. I use the rating feature in an admin name space
  3. the admin name space uses active scaffold for the controller and view logic.
  4. rails 2.3.8 and ruby 1.8.7, gem version is 2.2.8.1

I am able to display the global rating and under that the rating of that particular user using the following code in my view:

  
Avarage Rating:
{:url => ate_admin_user_path(@record)} %>
Your Rating:
true, :remote_options => {:url => rate_admin_user_path(@record)} %>

I do get the hover effect when mousing over the stars due to including <%= ajaxful_rating_style %> in my layout file. When clicking a star the rating is kind of indicated by keeping number of rated stars in the hover state (red/orange). The rating does take place due to my controller functions:

def rate
@record = User.find(params[:id])
@record.rate(params[:stars], current_user, params[:dimension])
render :update do |page|
  page.replace_html @record.wrapper_dom_id(params), ratings_for(@record, params.merge(:wrap => false))
  page.visual_effect :highlight, @record.wrapper_dom_id(params)
end
end

The rating is applied and the average is calculated which can be seen upon reloading the page. But the ajax part of updating the star rating upon click does not happen. Instead I do get the following error in the logs:

  NameError (Constant AjaxfulRating::MissingRateRoute from   ajaxful_rating/missing_rate_route.rb not found
  Constant MissingRateRoute from missing_rate_route.rb not found):
    desert (0.5.1) lib/desert/rails/dependencies.rb:56:in `look_for_constant_in_parent_module'
    desert (0.5.1) lib/desert/rails/dependencies.rb:13:in `load_missing_constant'
    /home/helmerj/.rvm/gems/ree-1.8.7-2010.02/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in `const_missing'
    /home/helmerj/.rvm/gems/ree-1.8.7-2010.02/gems/ajaxful_rating-2.2.8.1/lib/axr/stars_builder.rb:52:in `apply_stars_builder_options!'
    /home/helmerj/.rvm/gems/ree-1.8.7-2010.02/gems/ajaxful_rating-2.2.8.1/lib/axr/stars_builder.rb:10:in `initialize'
    /home/helmerj/.rvm/gems/ree-1.8.7-2010.02/gems/ajaxful_rating-2.2.8.1/lib/axr/helpers.rb:82:in `new'
    /home/helmerj/.rvm/gems/ree-1.8.7-2010.02/gems/ajaxful_rating-2.2.8.1/lib/axr/helpers.rb:82:in `ratings_for'
    app/controllers/admin/users_controller.rb:125:in `rate'
    app/controllers/admin/users_controller.rb:124:in `rate'
    haml (3.0.15) lib/sass/plugin/rack.rb:41:in `call'

    Rendered rescues/_trace (71.6ms)
    Rendered rescues/_request_and_response (0.5ms)
    Rendering rescues/layout (internal_server_error)

My routes for this rating looks like this:

  map.namespace :admin do |admin|
    admin.resources :users, :member => {:rate => :post}, :active_scaffold => true
  end

I am so close, any help on this would be greatly appreciated.

Cheers J.

helmerj avatar Aug 24 '10 09:08 helmerj

Using firebug I was able to get a more detailed error on the issue:

http://gist.github.com/547433

helmerj avatar Aug 24 '10 12:08 helmerj

This is because the code for the ajax callback does not work with nested routes.

My example routes.rb:

map.resources :users do |user| user.resource :portfolio do |portfolio| portfolio.resources :images, :member => {:rate => :post} end end

I believe the problem lies in lines 47-48 of stars_builder.rb:

rateable_name = ActionController::RecordIdentifier.singular_class_name(rateable) url = "rate_#{rateable_name}_path"

This assumes that your route is not nested. So url = "rate_image_path" instead of url = "rate_user_portfolio_image_path" (which is what it should be). I patched this by hardcoding the url, but I know this is not the best solution. Any thoughts on how to fix this to work with nested routes?

markphelps avatar Feb 09 '11 02:02 markphelps

Same problem here. Any updates on this? Or a good workaround?

hk6500 avatar May 02 '12 14:05 hk6500

Follow this wiki https://github.com/edgarjs/ajaxful-rating/wiki/Rails-3-View-Update-with-jQuery

crowdvn avatar Jun 12 '12 02:06 crowdvn

Thanks @crowdvn...it worked perfectly!!

Joseph-N avatar Feb 12 '13 14:02 Joseph-N

@crowdvn it also works for Rails 4 https://github.com/edgarjs/ajaxful-rating/wiki/Rails-3-and-4-View-Update-with-jQuery-(Update-rating-pages)

anduong avatar Dec 27 '13 04:12 anduong