Implementing Ajax - Rails App -
i'm trying implement ajax when favorite place, reload/update of section/ajax functionality doesn't work. if reload page, see action has occurred. appreciate tips.
my favorite_places/show.html.erb
<div class="heart"> <p class="text-center"> <% if current_user %> <%- unless current_user.favorite_places.exists?(id: @place.id) -%> <%= link_to favorite_places_path(place_id: @place), remote: true, method: :post %> <span title="favorite place"><i class="glyphicon glyphicon-heart-empty"></i></span> <% end %> <%- else -%> <%= link_to favorite_place_path(@place), remote: true, method: :delete %> <span title="un-favorite place"><i class="glyphicon glyphicon-heart"></i></span> <% end %> <% end %> <% end %> </p> </div>
my favorite_places_controller.rb
def update @favorite_place.update_attributes[params[:place]] respond_to |format| format.html { redirect_to @place } format.js end end
my update.js.erb
<% if @place.favorited %> $(' #<%= @place.id %>').html("true"); console.log ("<%= @place.name %> has been favorited."); <% else %> $(' #<%= @place.id %>').html("false"); console.log ("<%= @place.name %> not favorite."); <% end %>
my dev console output
delete http://0.0.0.0:3000/favorite_places/34 406 (not acceptable) jquery.js?body=1:8716send jquery.js?body=1:8716jquery.extend.ajax jquery.js?body=1:8146$.rails.rails.ajax jquery_ujs.js?body=1:73$.rails.rails.handleremote jquery_ujs.js?body=1:149(anonymous function) jquery_ujs.js?body=1:299jquery.event.dispatch jquery.js?body=1:5108elemdata.handle
ruby-on-rails ajax
No comments:
Post a Comment