1

I need to implement Star Rating system to let users rate hotels. I took this tutorial as the example.

rating.js file is:

$(function() {
  $('.rating_star').click(function() {
    var star = $(this);
    var form_id = star.attr("data-form-id");
    var stars = star.attr("data-stars");
    $('#' + form_id + '_stars').val(stars);

    $.ajax({
      type: "post",
      url: $('#' + form_id).attr('action') + '.json',
      data: $('#' + form_id).serialize(),
      success: function(response){
        console.log(response);
        update_stars();
        if(response["avg_rating"]){
          $('#average_rating').text(response["avg_rating"]);
          }
        }
      })
  });        
});

function update_stars(){
  $('.star_rating_form').each(function() {
    var form_id = $(this).attr('id');
    set_stars(form_id, $('#' + form_id + '_stars').val());
  });
}

function set_stars(form_id, stars) {
  for(i = 1; i <= 5; i++){
    if(i <= stars){
      $('#' + form_id + '_' + i).addClass("on");
    } else {
      $('#' + form_id + '_' + i).removeClass("on");
    }
  }
}

But this code doesn't work, because when I mouse-on or click the stars, nothing happens at all... I think the error is somewhere in the java code, because testing javascript file works fine. But I can't find this error.

PS: ratings_controller.rb

  before_filter :js_logged_in

  def create
    @rating = Rating.new(params[:rating])
    @hotel = Hotel.find(params[:rating][:hotel_id])

    respond_to do |format|
        if @rating.save
          format.json { render :json => { :avg_rating => @hotel.avg_rating } }
        else
          format.json { render :json => @rating.errors, :status => :unprocessable_entity }
        end
    end
  end

  def update
    @rating = Rating.find(params[:id])
    @hotel = Hotel.find(params[:rating][:hotel_id])
    @rating.update_attributes(params[:rating])

    respond_to do |format|
      if @rating.save
        format.json { render :json => { :avg_rating => @hotel.avg_rating } }
      else
        format.json { render :json => @rating.errors, :status => :unprocessable_entity }
      end
    end
  end

  def rating_params
    params.require(:rating).permit(:stars)
  end

  private

    def js_logged_in
      if(!signed_in?)
      flash[:error] = "You must be a signed in to leave a rating!"
      render :js => "window.location = '/signin'"
    end
end

Source of the page with rating stars consists this line:

<script data-turbolinks-track="true" src="/assets/rating.js?body=1"></script>

UPD1: Output of the rails error log showed me that I missed an end statement... After I added it the error changed:

Started PATCH "/ratings/2.json" for 127.0.0.1 at 2014-07-19 18:52:43 +0400
Processing by RatingsController#update as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"7DwBNYlmmucwodZ8UDINYIaMG8Ep4sloBGEoiaiXoxQ=", "rating"=>{"hotel_id"=>"10", "user_id"=>"1", "stars"=>"5"}, "id"=>"2"}
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."remember_token" = '715016a7327649b3aaeb6f7e65b10996cbe1eb08' LIMIT 1
  Rating Load (0.2ms)  SELECT  "ratings".* FROM "ratings"  WHERE "ratings"."id" = ? LIMIT 1  [["id", 2]]
  Hotel Load (0.1ms)  SELECT  "hotels".* FROM "hotels"  WHERE "hotels"."id" = ?  ORDER BY created_at DESC LIMIT 1  [["id", 10]]
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 500 Internal Server Error in 21ms

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
  app/controllers/ratings_controller.rb:20:in `update'


  Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.9ms)
  Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.1ms)
  Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (5.2ms)
  Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (72.6ms)

Error appears to be in rating_controller in the next line:

@rating.update_attributes(params[:rating])
13
  • What do you mean by "I think the error is exactly in the java code"? What is the output of browser console? Are there any JS errors? Commented Jul 21, 2014 at 13:27
  • Sorry, I meant 'somewhere'. No, there are no errors in console. Commented Jul 21, 2014 at 13:29
  • 1
    Can you show the console log for this error? Commented Jul 21, 2014 at 13:37
  • 1
    Sorry i wasn't clear. The running error log is what i meant. Basically the terminal window where you write 'rails server' When you click the star you are getting an internal server error. Let us know if there are error in that log Commented Jul 21, 2014 at 14:01
  • 1
    @AlMacks please paste whole output in question. Commented Jul 21, 2014 at 14:21

1 Answer 1

1

The error you're getting indicates you've missed an end statement at the end of the your file.

This is likely because your indenting is wrong, and has presumably has lead to unbalanced end statements.

Here is the end of your file:

  def js_logged_in
    if(!signed_in?)
    flash[:error] = "You must be a signed in to leave a rating!"
    render :js => "window.location = '/signin'"
  end
end

That final end doesn't close the class definition, it closes def js_logged_in method. You didn't indent the body of your if, the end that matches your def js_logged_in is actually closing the if.

Sign up to request clarification or add additional context in comments.

2 Comments

You're right, but there is a new mistake now (after I added 'end').
UPD ...and it gone when I changed the line into this: @rating.update_attributes(rating_params). Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.