weixin_33674437 2014-10-13 04:15 采纳率: 0%
浏览 9

实施Ajax-Rails应用

I'm trying to implement ajax into when I favorite a place, but the reload/update of the section/ajax functionality doesn't work. Only if I reload the page, then I see the action that has occurred. Would appreciate any 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 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>

My 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

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 %> is not a 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
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥150 如图所示配置eNSP
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改
    • ¥20 wireshark抓不到vlan
    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
    • ¥15 stata安慰剂检验作图但是真实值不出现在图上
    • ¥15 c程序不知道为什么得不到结果
    • ¥15 键盘指令混乱情况下的启动盘系统重装