我正在尝试基于https://medium.com/@adnama.lin/live-messaging-with-rails-5-action-cable-7f009e0c1d8b创建一个聊天应用程序,所以当用户点击用户的链接,而不是打开新页面将他带到聊天视图我试图在div中加载视图,所以希望你帮忙,随意问......
更新:$(".chat").load($(this).attr("href"));
将无法工作,因为我想加载整个链接与它的data-method = post
等...
<% @users.each do |other_user| %>
<div> <%= link_to(user_chats_path(current_user, :other_user => other_user.id), method: :post, :class => "link") do %>
<%= other_user.username %>
<% end %>
<% end %>
这是jquery脚本:
<script>
$(".link").on("click",function {
$(".chat").load($(this));
return false;
});
</script>
load()
需要一个url字符串,但是你传递一个jQuery对象。从链接获取href并使用它。
您还需要阻止浏览器使用event.preventDefault()
关注该链接
尝试改为
$(".link").on("click",function(event) {
event.preventDefault()
$(".chat").load(this.href);
});