weixin_33716557 2018-11-22 16:47 采纳率: 0%
浏览 18

Rails Ajax问题

I am trying to add a liking functionality to a place object that I have created in rails -- I'm using Actsasvotable and have no issue adding/deleting likes from the database. However, I am now stuck and can't figure out how to add the like without a page load.

Here is my view:

<div class="place-likes" id="place-<%= place.id %>.likes">
        <%= render 'places/likes', place: place %>
    </div>  

Which includes the following partial:

<%= render 'shared/likes_form', likeable: place %>
<div class="place-<%= place.id %>.like-count"><%= likers_of(place) %></div>

Which calls the shared likes form (using for other objects other than places):

<% if current_user.liked? likeable %>
  <%= form_tag unlike_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), method: :post, remote: true, class: 'unlike' do %>
    <% button_tag class: "glyphicon glyphicon-heart" do %>
    <% end %>    
  <% end %>
<% else %>
  <%= form_tag like_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), remote: true, class: 'like' do %>
    <% button_tag class: "glyphicon glyphicon-heart-empty" do %>
    <% end %>    
  <% end %>
<% end %>

Once the controller saves the like into the database, it returns the following javascript. I have checked closely that this javascript matches the correct object using the console, but I the .html part will not work whatever I do:

$("#<%= @likeable_type.downcase %>-<%= @likeable.id %>.likes").html("<%= j (render partial: 'places/likes', locals: { place: @likeable } ) %>");

Any help here would be greatly appreciated! Thanks!!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 linux驱动,linux应用,多线程