0

I am trying to update a rails partial while using Ajax and having some trouble. Basically I have a Post with a Karma attribute. Users can vote up and vote down to take and remove Karma from the post. I would like to allow the user to asynchronously update the post value using Ajax and im running into a NilClass error that im hoping you can help me with. The error im getting is undefined method `karma' for nil:NilClass (in my _karma_value.erb file), and my files look like below. Im totally stuck!

posts_controller.rb

   def add_karma
     # Save the page that the request came from
     session[:initial_page] ||= request.referer

     @post = Post.find(params[:id])
     @post.karma = @post.karma + 1
     @post.update_attributes(params[:post])

     respond_to do |format|
       format.html { redirect_to session[:initial_page] }  #redirect back to requesting page
       format.json { head :no_content }
       format.js {}
     end   
   end

add_karma.js.erb

   $('#add_karma').html(
     "<%= escape_javascript(render('_karma_value', post: @post)) %>");

index.html.erb

...
<b>Karma:</b> 
        <%= render(:partial => 'karma_value', :locals => {:post => @post}) %>  <br>
...

_karma_value.erb

<% if !(post.karma.nil?) %>
    <%= post.karma %>
<% end %>
Salman
  • 494
  • 4
  • 18

3 Answers3

1

Yes, @post will be nil in the partial.

Because, while rendering partial, you have mentioned the local variable post as @post. But using @post in partial.

Just use post in that partial.

in _karma_value.html.erb

<% if !(post.karma.nil?) %>
  <%= post.karma %>
<% end %>
Sagar Bommidi
  • 1,409
  • 8
  • 12
  • I tried that before, and tried it again after your comment - but I get the same error message: undefined method `karma' for nil:NilClass – Salman Mar 20 '13 at 14:58
  • once can you check by modifying the content of the **add_karma.js.erb** file like `$('#add_karma').html("<%= render(:partial => 'karma_value', :locals => {:post => @post}) %>")`. – Sagar Bommidi Mar 20 '13 at 17:13
  • Ok I did this and it worked - (I no longer am receiving the undefined method error) I think the problem was I still had the underscore in front of karma_value - but now that its rendering properly - I cannot get the partial to refresh (i.e. the value stays the same until I refresh the entire page). Any Ideas? – Salman Mar 21 '13 at 16:47
  • U didnt mentioned the DOM element having id as **add_karma** in **index.html.erb**. If you are updating the proper element in the **.js.erb** file, then surely it will reflect. – Sagar Bommidi Mar 21 '13 at 17:44
  • OMG this totally worked! I was completely misunderstanding the javascript and thought that the $('#add_karma').html was referring to the controller action it was tied to and not the DOM element. Adding the add_karma div to the DOM totally solved the problem. Thank you! – Salman Mar 21 '13 at 19:16
  • Hrmmm... interestingly enough - this always updates the first post element in the index file (not the same element that I click). Will investigate further. – Salman Mar 21 '13 at 19:57
0

you pass :locals => {:post => @post} so you should use post instead of @post inside _karma_value.erb partial

Yuri Golobokov
  • 1,829
  • 12
  • 11
  • I tried that before, and tried it again after your comment - but I get the same error message: undefined method `karma' for nil:NilClass - nothing else changes. – Salman Mar 20 '13 at 14:59
  • then `@post` is nil at index.html.erb. Check index action. – Yuri Golobokov Mar 20 '13 at 15:01
0

Why don't you use the same exact render :partial => command in both add_karma.js.erb and index.html.erb? It seems that you are not rendering correctly the partial in the add_karma.js.erb.

Sébastien Saunier
  • 1,787
  • 2
  • 19
  • 28