dnyce
dnyce

Reputation: 415

Rails ajax star rating not returning rating on initial update

I'm trying to create an AJAX star rating form on my site. Using the tutorial found on this site, I managed to get close. However, whenever I try to submit an update to the rating form, by clicking another radio button, my current_user rating value for the object is returning N/A instead of the correct value. I've followed the tutorial to the T and I'm a bit confused why this is happening.

Has anyone run into this when creating something similar?

Here is my rating form code:

<%
    content_for(:scripts) do
        javascript_include_tag 'public/rating_ballot'
    end 
%>

<%= form_for rating_ballot, remote: true, html: { class: "rating_ballot" } do |f| %>
    <%= f.label "value_1", content_tag(:span, '1'), { class: 'rating', id: '1' } %>
    <%= radio_button_tag("rating[value]", 1, current_user_rating == 1, class: 'rating_button') %>
    <%= f.label "value_2", content_tag(:span, '2'), { class: 'rating', id: '2' } %>
    <%= radio_button_tag "rating[value]", 2, current_user_rating == 2, class: 'rating_button' %>
    <%= f.label "value_3", content_tag(:span, '3'), { class: 'rating', id: '3' } %>
    <%= radio_button_tag "rating[value]", 3, current_user_rating == 3, class: 'rating_button' %>
    <%= f.label "value_4", content_tag(:span, '4'), { class: 'rating', id: '4' } %>
    <%= radio_button_tag "rating[value]", 4, current_user_rating == 4, class: 'rating_button' %>
    <%= f.label "value_5", content_tag(:span, '5'), { class: 'rating', id: '5' } %>
    <%= radio_button_tag "rating[value]", 5, current_user_rating == 5, class: 'rating_button' %>

    <%= hidden_field_tag "game_id", @game.id %>
    <%= f.submit :submit  %>
<% end %>

Here is my update.js.erb file:

$('#rating').replaceWith("<%= escape_javascript(render partial: 'games/ratings-test') %>");

Here are my helper methods:

def rating_ballot
    if @rating = current_user.ratings.find_by_game_id(params[:id])
        @rating
    else
        current_user.ratings.new
    end
end

def current_user_rating
    if @rating = current_user.ratings.find_by_game_id(params[:id])
        @rating.value
    else
        "N/A"
    end
end

Here is an example return when I click on the first radio button on initial submit.

    <table id="rating">
                        <thead>
                            <tr>
                                <th colspan="2">Game Ratings</th>
                            </tr>
                        </thead>
                        <tbody><tr>
                            <td>Average Rating</td>
                            <td>1.0</td>
                        </tr>
                        <tr>
                            <td>Your Rating</td>
                            <td>N/A</td>
                        </tr>
                    </tbody>
</table>

Here's the output upon page refresh AFTER a submit:

<table id="rating">
                    <thead>
                        <tr>
                            <th colspan="2">Game Ratings</th>
                        </tr>
                    </thead>
                    <tbody><tr>
                        <td>Average Rating</td>
                        <td>1.0</td>
                    </tr>
                    <tr>
                        <td>Your Rating</td>
                        <td>1</td>
                    </tr>
                </tbody>
</table>

It's odd, it's almost as if Rails is not picking up on my current_user until I do a page refresh.

Upvotes: 1

Views: 171

Answers (1)

dnyce
dnyce

Reputation: 415

Ok, after much poking and prodding, I managed to get this working by dropping a "locals" param into the update.js.erb file like so:

locals: {current_user_rating: params['rating']['value']}

My new update.js.erb code looks like this:

$('#rating').replaceWith("<%= escape_javascript(render partial: 'games/ratings-test', locals: {current_user_rating: params['rating']['value']}) %>");

Upvotes: 1

Related Questions