Rails 4 Ajax 点赞按钮无法正常工作
Rails 4 Ajax like button failing to wоrk
我正在努力让我的 Like link 与 ajax 一起工作。点击后"Like"没有任何变化,只有刷新页面后我才能看到点赞。
编辑:添加路线。
EDIT2:我用 Firebug 检查了 Like link 并在输入 like.js.erb 文件中的代码后显示消息:
Image
我的 like.js.erb 文件:
<% if Like.find_by(likeable: @post) %>
$(".likes").html("<%= escape_javascript(render 'posts/unlike' ) %>");
<% else %>
$(".likes").html("<%= escape_javascript(render 'posts/like' ) %>");
<% end %>
(我已经检查过,如果条件正常)。
我偏爱 posts (_post.html.erb).
<li class="post">
<span class="user"><%= link_to post.user.name, post.user %></span>
<br>
<span class="title"><%= post.title %></span>
<br>
<span class="ava"><%= image_tag post.avatar(:large) %></span>
<br>
<span class="timestamp">
Posted <%= time_ago_in_words(post.created_at) %> ago.
<% if logged_in? && current_user.admin? %>
<%= link_to "delete", post, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
################## START OF THE LIKES #####################
<span class="likes">
<span class="like">
<% if logged_in? %>
<% if !current_user.likes?(current_user, post) %>
<%= render 'posts/like', post: post %>
<!-- <%= link_to "Like", like_post_path(post, like: true), method: 'post', remote: true %> -->
</span>
<% else %>
<span class="unlike">
<%= render 'posts/unlike', post: post %>
<!-- <%= link_to "Unlike", like_post_path(post, like: false), method: 'post', remote: true %>-->
</span>
<% end %>
(<%= pluralize( post.likes.size, "like") %>)
<% else %>
<%= link_to "Like", login_path,
data: { confirm: "You need to log in to like" } %>
(<%= pluralize( post.likes.size, "like") %>)
<% end %>
</span>
################## THE END OF THE LIKES #####################
</span>
</li>
以及 post 控制器中的类似方法。
def like
post = Post.find(params[:id])
if Like.find_by(likeable: post)
Like.find_by(likeable: post).destroy
respond_to do |format|
format.html do
flash[:success] = "Like Updated!"
redirect_to :back
end
format.js
end
else
Like.create(likeable: post, user: current_user, like: params[:like])
respond_to do |format|
format.html do
flash[:success] = "Like Updated!"
redirect_to :back
end
format.js
end
end
end
我使用的类似和不同的偏音:
喜欢:
<%= link_to "Like", like_post_path(post, like: true), method: 'post', remote: true %>
不喜欢:
<%= link_to "Like", like_post_path(post, like: false), method: 'post', remote: true %>
任何帮助或建议都会很好。我知道我的代码状态很差,所以不要害怕对它提出一些修改建议。最后,请帮助我,我已经浪费了大量时间来尝试让它发挥作用,但还没有结果。
路线:
Rails.application.routes.draw do
root 'main_page#home'
get 'top_list' => 'main_page#top_list'
get 'sign_up' => 'users#new'
get 'posts/new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :posts do
member do
post 'like'
end
end
end
点赞后有日志
Completed 500 Internal Server Error in 66ms (ActiveRecord: 41.0ms)
NameError (undefined local variable or method `post' for #<#<Class:0x007f8ce976ff48>:0x007f8ce976f3e0>):
app/views/posts/_unlike.html.erb:1:in `_app_views_posts__unlike_html_erb___187529012528174235_70121703666860'
app/views/posts/like.js.erb:4:in `_app_views_posts_like_js_erb__3375438513772599537_70121703368540'
app/controllers/posts_controller.rb:38:in `like'
尝试在末尾添加 html_safe
。另外,我认为渲染也需要 post
(就像您在视图中所做的那样)
$(".likes").html('<%= escape_javascript(render "posts/unlike",post: @post).html_safe %>');
我正在努力让我的 Like link 与 ajax 一起工作。点击后"Like"没有任何变化,只有刷新页面后我才能看到点赞。
编辑:添加路线。 EDIT2:我用 Firebug 检查了 Like link 并在输入 like.js.erb 文件中的代码后显示消息: Image 我的 like.js.erb 文件:
<% if Like.find_by(likeable: @post) %>
$(".likes").html("<%= escape_javascript(render 'posts/unlike' ) %>");
<% else %>
$(".likes").html("<%= escape_javascript(render 'posts/like' ) %>");
<% end %>
(我已经检查过,如果条件正常)。 我偏爱 posts (_post.html.erb).
<li class="post">
<span class="user"><%= link_to post.user.name, post.user %></span>
<br>
<span class="title"><%= post.title %></span>
<br>
<span class="ava"><%= image_tag post.avatar(:large) %></span>
<br>
<span class="timestamp">
Posted <%= time_ago_in_words(post.created_at) %> ago.
<% if logged_in? && current_user.admin? %>
<%= link_to "delete", post, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
################## START OF THE LIKES #####################
<span class="likes">
<span class="like">
<% if logged_in? %>
<% if !current_user.likes?(current_user, post) %>
<%= render 'posts/like', post: post %>
<!-- <%= link_to "Like", like_post_path(post, like: true), method: 'post', remote: true %> -->
</span>
<% else %>
<span class="unlike">
<%= render 'posts/unlike', post: post %>
<!-- <%= link_to "Unlike", like_post_path(post, like: false), method: 'post', remote: true %>-->
</span>
<% end %>
(<%= pluralize( post.likes.size, "like") %>)
<% else %>
<%= link_to "Like", login_path,
data: { confirm: "You need to log in to like" } %>
(<%= pluralize( post.likes.size, "like") %>)
<% end %>
</span>
################## THE END OF THE LIKES #####################
</span>
</li>
以及 post 控制器中的类似方法。
def like
post = Post.find(params[:id])
if Like.find_by(likeable: post)
Like.find_by(likeable: post).destroy
respond_to do |format|
format.html do
flash[:success] = "Like Updated!"
redirect_to :back
end
format.js
end
else
Like.create(likeable: post, user: current_user, like: params[:like])
respond_to do |format|
format.html do
flash[:success] = "Like Updated!"
redirect_to :back
end
format.js
end
end
end
我使用的类似和不同的偏音: 喜欢:
<%= link_to "Like", like_post_path(post, like: true), method: 'post', remote: true %>
不喜欢:
<%= link_to "Like", like_post_path(post, like: false), method: 'post', remote: true %>
任何帮助或建议都会很好。我知道我的代码状态很差,所以不要害怕对它提出一些修改建议。最后,请帮助我,我已经浪费了大量时间来尝试让它发挥作用,但还没有结果。
路线:
Rails.application.routes.draw do
root 'main_page#home'
get 'top_list' => 'main_page#top_list'
get 'sign_up' => 'users#new'
get 'posts/new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :posts do
member do
post 'like'
end
end
end
点赞后有日志
Completed 500 Internal Server Error in 66ms (ActiveRecord: 41.0ms)
NameError (undefined local variable or method `post' for #<#<Class:0x007f8ce976ff48>:0x007f8ce976f3e0>):
app/views/posts/_unlike.html.erb:1:in `_app_views_posts__unlike_html_erb___187529012528174235_70121703666860'
app/views/posts/like.js.erb:4:in `_app_views_posts_like_js_erb__3375438513772599537_70121703368540'
app/controllers/posts_controller.rb:38:in `like'
尝试在末尾添加 html_safe
。另外,我认为渲染也需要 post
(就像您在视图中所做的那样)
$(".likes").html('<%= escape_javascript(render "posts/unlike",post: @post).html_safe %>');