我正在使用" acts_as_votable"我的Rails应用程序中的gem,以便用户可以对帖子进行投票。我尝试添加Ajax功能,以便在用户提交帖子时不必刷新整个页面。
我的upvote / downvote链接是可点击的并记录投票,但渲染没有刷新,投票计数仍然保持不变(我仍然需要刷新页面以查看计数更改)。
我可以在运行日志中看到我的错误:
Rendered posts/downvote.js.erb (3.4ms)
Completed 500 Internal Server Error in 77ms (ActiveRecord: 8.4ms)
ActionView::Template::Error (undefined method `id' for nil:NilClass):
1: $(document).ready(function(){
2: $('#vote_<%= p.id %>').html("<%= escape_javascript render :partial => 'vote' %>");
3: });
app/views/posts/downvote.js.erb:2:in `_app_views_posts_downvote_js_erb__811382410496335987_70314739481440'
app/controllers/posts_controller.rb:44:in `downvote'
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.0ms)
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.7ms)
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (17.0ms)
这就是我所拥有的:
的routes.rb
resources :posts do
member do
put "like", to: "posts#upvote"
put "dislike", to: "posts#downvote"
end
end
posts_controller.rb
def upvote
@post = Post.find(params[:id])
@post.upvote_by current_user
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
def downvote
@post = Post.find(params[:id])
@post.downvote_by current_user
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
explore.html.erb (/views/pages/explore.html.erb)
<% if @posts.each do |p| %>
<div class="panel-body">
<p class="post-content"><%= auto_link(p.content, :html => { :target => '_blank' }) %></p>
<%= render :partial => '/posts/vote', :class => "vote", :locals => { p: p } %>
</div>
<% end %>
_vote.html.erb (/views / postts / _vote.html.erb)
<<div id="vote_<%= p.id %>" class="vote">
<%= link_to 'Vote Up', like_post_path(p), :class => "upvote", :method => :put, :remote => true %>
<span><%= p.score %></span> <!--show the current vote-->
<%= link_to 'Vote Down', dislike_post_path(p), :class => "downvote", :method => :put, :remote => true %>
</div>
upvote.js.erb (/views / postts / upvote.js.erb)
$(document).ready(function(){
$('#vote_<%= p.id %>').html("<%= escape_javascript render :partial => 'vote' %>");
});
downvote.js.erb (/views / postts / downvote.js.erb)
$(document).ready(function(){
$('#vote_<%= p.id %>').html("<%= escape_javascript render :partial => 'vote' %>");
});
我的javascript有问题,还是我渲染错了?提前谢谢。
答案 0 :(得分:2)
错误中有答案,您的javascript文件中的 您能够在html文件中使用p的原因是因为您循环遍历@posts集合,在名为p的变量中提供每个帖子。 但是在你的js文件中你没有这样的循环,因此你需要直接访问实例变量。 另外,当你在javascript文件中渲染_vote部分时,你没有提供 将downvote.js.erb代码更改为:p
变量为零是p
局部变量,你也需要包含它。$(document).ready(function(){
$('#vote_<%= @post.id %>').html("<%= escape_javascript(render(:partial =>'vote', :locals => { p: @post })) %>");
});
答案 1 :(得分:2)
_vote.html.erb (/views/posts/_vote.html.erb)第一行中的“&lt;&lt; ”将会增加语法错误。
在* .js.erb文件中,只有来自控制器操作的 @post ,而不是视图中的 p 。尝试更新如下:
upvote.js.erb (/views / postts / upvote.js.erb)
$(document).ready(function(){
$('#vote_<%= @post.id %>').html("<%= escape_javascript render :partial => 'vote', locals: { p: @post } %>");
});
downvote.js.erb (/views / postts / downvote.js.erb)
$(document).ready(function(){
$('#vote_<%= @post.id %>').html("<%= escape_javascript render :partial => 'vote', locals: { p: @post } %>");
});
希望这有帮助。 :)