Using jquery and ajax to update a partial using Ruby on Rails -


i've had issue days , been unable figure out... have 'like' , 'unlike' button. when user "likes" post, updates should occur in data base , button should flip "unlike". works on backend - database updates fine, button never switches "like" "unlike". if manually refresh page switched, doesn't happen without refresh. not have errors logged or in console.

shared/_like.html.erb

<%= form_for(current_user.likes.build, remote: true) |f| %> <div><%= hidden_field_tag :post_id, post.id %></div> <%= f.submit "like", class: "btn btn-primary" %> <% end %> 

shared/_unlike.html.erb

<%= form_for(current_user.likes.find_by_post_id(post.id), html: {method: :delete }, remote: true) |f| %> <%= f. submit "unlike", class: "btn" %> <% end %> 

these rendered through: post/_post.html.erb

<div id="like_form">     <% if post.does_user_like(post.id, current_user.id) %>         <%= render 'shared/unlike', :post => post %>     <% else %>                           <%= render 'shared/like', :post => post %>     <% end %> </div>   

likes/create.js.erb

$("#like_form").html("<%= escape_javascript(render('shared/unlike', :post => @post)) %>"); 

likes controller

def create       user = current_user           post = post.find(params[:post_id])       @post = post       like.create(post_id: post.id, user_id: user.id)            respond_to |format|             format.html { redirect_to request.referrer || root_url }             format.js            end   end 

use replacewith instead of html in likes/create.js.erb


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -