我正在尝试在我最喜欢的地方实现ajax,但是部分/ ajax功能的重新加载/更新不起作用。只有当我重新加载页面时,我才会看到已经发生的操作。非常感谢任何提示。
我的 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 do %>
<span title="Favorite this Place"><i class="glyphicon glyphicon-heart-empty"></i></span>
<% end %>
<%- else -%>
<%= link_to favorite_place_path(@place), remote: true, method: :delete do %>
<span title="Un-Favorite this Place"><i class="glyphicon glyphicon-heart"></i></span>
<% end %>
<% end %>
<% end %>
</p>
</div>
我的 favorite_places_controller.rb
def update
@favorite_place.update_attributes[params[:place]]
respond_to do |format|
format.html { redirect_to @place }
format.js
end
end
我的 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 %> is not a favorite.");
<% end %>
我的开发者控制台输出
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