我有一个提示,要求使用'redirect_to'和'update_vote!'在两行中编写我的up_vote和down_vote方法。方法如下所示。实现redirect_to很容易,但我不太确定如何使用现有的'update_vote!'简明地编写我的up / down_vote方法。方法。任何帮助表示赞赏。
class VotesController < ApplicationController
before_action :load_post_and_vote
def up_vote
if @vote
@vote.update_attribute(:value, 1)
else
@vote = current_user.votes.create(value: 1, post: @post)
end
# http://apidoc.com/rails/ActionController/Base/redirect_to
redirect_to :back
end
def down_vote
if @vote
@vote.update_attribute(:value, -1)
else
@vote = current_user.votes.create(value: -1, post: @post)
end
# http://apidoc.com/rails/ActionController/Base/redirect_to
redirect_to :back
end
private
def load_post_and_vote
@post = Post.find(params[:post_id])
@vote = @post.votes.where(user_id: current_user.id).first
end
def update_vote!(new_value)
if @vote
authorize @vote, :update?
@vote.update_attribute(:value, new_value)
else
@vote = current_user.votes.build(value: new_value, post: @post)
authorize @vote, :create
@vote.save
end
end
end
答案 0 :(得分:2)
您应该调用update_vote!
方法。怎么样:
def up_vote
update_vote!(1)
# http://apidoc.com/rails/ActionController/Base/redirect_to
redirect_to :back
end
def down_vote
update_vote!(-1)
# http://apidoc.com/rails/ActionController/Base/redirect_to
redirect_to :back
end
此外,您的方法可以重写为:
def update_vote!(new_value)
Vote.find_or_create_by post: @post do |v|
authorize v, :update?
v.user_id = current_user.id
v.value = new_value
v.save!
end
end
答案 1 :(得分:-1)
似乎答案很容易=]
def up_vote
update_vote(1)
redirect_to :back
end
def down_vote
update_vote(-1)
redirect_to :back
end