acts_as_votable ajax 在刷新整个页面之前不更新数据
acts_as_votable ajax not updating the data until I refresh the whole page
嗨,我是 rails
的新手
实际上投票工作正常,但在我刷新整个页面之前它不会更新数据
这是我投票时的错误
这是我的代码
routes.rb
resources :posts do
match :vote, via: [:post, :delete], on: :member
end
controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy, :vote]
respond_to :js, :html, :json
def vote
@post = Post.find(params[:id])
if request.post?
@post.upvote_from current_user
elsif request.delete?
@post.downvote_from current_user
end
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
vote.js.erb
$("<%=j @post.id %>").html("<%=j render partial: "posts/vote", locals: { post: @post } %>");
_vote.html.erb
<% voted = current_user.voted_for? post %>
<% styling = voted ? "" : "un" %>
<% arrow = voted ? "up" : "down" %>
<% method = voted ? :delete : :post %>
<%= link_to vote_post_path(post), method: method, remote: true, class: '#
{styling}like_post', id: post.id do %>
<button type="button" class="btn btn-info" aria-label="Left Align">
<i class="fa fa-arrow-<%= arrow %>"></i>
<span class="badge vote_count"><%= post.get_upvotes.size %></span>
</button>
<% end %>
show.html.erb
<%= render partial: "vote", locals: { post: @post } %>
application.js
//= require rails-ujs
//= require turbolinks
//= require_tree .
//= require jquery3
//= require popper
//= require bootstrap-sprockets
$("<%= @post.id %>").html("<%=j render partial: "posts/vote", locals: { post: @post } %>");
如果它不是 script/text,则不需要转义 j。假设 post 的 ID 为 5,因此出现错误
undefined method `gsub' for 5:Integer
嗨,我是 rails
的新手实际上投票工作正常,但在我刷新整个页面之前它不会更新数据
这是我投票时的错误
这是我的代码
routes.rb
resources :posts do
match :vote, via: [:post, :delete], on: :member
end
controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy, :vote]
respond_to :js, :html, :json
def vote
@post = Post.find(params[:id])
if request.post?
@post.upvote_from current_user
elsif request.delete?
@post.downvote_from current_user
end
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
vote.js.erb
$("<%=j @post.id %>").html("<%=j render partial: "posts/vote", locals: { post: @post } %>");
_vote.html.erb
<% voted = current_user.voted_for? post %>
<% styling = voted ? "" : "un" %>
<% arrow = voted ? "up" : "down" %>
<% method = voted ? :delete : :post %>
<%= link_to vote_post_path(post), method: method, remote: true, class: '#
{styling}like_post', id: post.id do %>
<button type="button" class="btn btn-info" aria-label="Left Align">
<i class="fa fa-arrow-<%= arrow %>"></i>
<span class="badge vote_count"><%= post.get_upvotes.size %></span>
</button>
<% end %>
show.html.erb
<%= render partial: "vote", locals: { post: @post } %>
application.js
//= require rails-ujs
//= require turbolinks
//= require_tree .
//= require jquery3
//= require popper
//= require bootstrap-sprockets
$("<%= @post.id %>").html("<%=j render partial: "posts/vote", locals: { post: @post } %>");
如果它不是 script/text,则不需要转义 j。假设 post 的 ID 为 5,因此出现错误
undefined method `gsub' for 5:Integer