Note that there are some explanatory texts on larger screens.

plurals
  1. PORails ajax star rating not returning rating on initial update
    primarykey
    data
    text
    <p>I'm trying to create an AJAX star rating form on my site. Using the tutorial found on <a href="http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails-3" rel="nofollow">this site</a>, 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.</p> <p>Has anyone run into this when creating something similar?</p> <p>Here is my <strong>rating form</strong> code:</p> <pre><code>&lt;% content_for(:scripts) do javascript_include_tag 'public/rating_ballot' end %&gt; &lt;%= form_for rating_ballot, remote: true, html: { class: "rating_ballot" } do |f| %&gt; &lt;%= f.label "value_1", content_tag(:span, '1'), { class: 'rating', id: '1' } %&gt; &lt;%= radio_button_tag("rating[value]", 1, current_user_rating == 1, class: 'rating_button') %&gt; &lt;%= f.label "value_2", content_tag(:span, '2'), { class: 'rating', id: '2' } %&gt; &lt;%= radio_button_tag "rating[value]", 2, current_user_rating == 2, class: 'rating_button' %&gt; &lt;%= f.label "value_3", content_tag(:span, '3'), { class: 'rating', id: '3' } %&gt; &lt;%= radio_button_tag "rating[value]", 3, current_user_rating == 3, class: 'rating_button' %&gt; &lt;%= f.label "value_4", content_tag(:span, '4'), { class: 'rating', id: '4' } %&gt; &lt;%= radio_button_tag "rating[value]", 4, current_user_rating == 4, class: 'rating_button' %&gt; &lt;%= f.label "value_5", content_tag(:span, '5'), { class: 'rating', id: '5' } %&gt; &lt;%= radio_button_tag "rating[value]", 5, current_user_rating == 5, class: 'rating_button' %&gt; &lt;%= hidden_field_tag "game_id", @game.id %&gt; &lt;%= f.submit :submit %&gt; &lt;% end %&gt; </code></pre> <p>Here is my <strong>update.js.erb</strong> file:</p> <pre><code>$('#rating').replaceWith("&lt;%= escape_javascript(render partial: 'games/ratings-test') %&gt;"); </code></pre> <p>Here are my <strong>helper methods</strong>:</p> <pre><code>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 </code></pre> <p>Here is an example return when I click on the first radio button on initial submit.</p> <pre><code> &lt;table id="rating"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th colspan="2"&gt;Game Ratings&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td&gt;Average Rating&lt;/td&gt; &lt;td&gt;1.0&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Your Rating&lt;/td&gt; &lt;td&gt;N/A&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>Here's the output upon page refresh AFTER a submit:</p> <pre><code>&lt;table id="rating"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th colspan="2"&gt;Game Ratings&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td&gt;Average Rating&lt;/td&gt; &lt;td&gt;1.0&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Your Rating&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>It's odd, it's almost as if Rails is not picking up on my current_user until I do a page refresh.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload