我有一个奇怪的情况.我的routes.rb中有这样的代码:
concern :votable do
post :vote_up, on: :member, controller: :votes
post :vote_down, on: :member, controller: :votes
end
resources :questions, concerns: [:commentable, :votable], shallow: true do
resources :answers, concerns: [:commentable, :votable]
end
Run Code Online (Sandbox Code Playgroud)
所以,它给了我帮助,vote_up_question并vote_up_answer通过'post':
vote_up_answer POST /answers/:id/vote_up(.:format) votes#vote_up
vote_down_answer POST /answers/:id/vote_down(.:format) votes#vote_down
vote_up_question POST /questions/:id/vote_up(.:format) votes#vote_up
vote_down_question POST /questions/:id/vote_down(.:format) votes#vote_down
Run Code Online (Sandbox Code Playgroud)
我的votes_controller:
before_action :load_parent
def vote_up
current_user.vote_for(@parent)
redirect_to :back, notice: "Voted up"
end
private
def load_parent
resource, id = request.path.split("/")[1, 2]
@parent = resource.singularize.classify.constantize.find(id)
end
Run Code Online (Sandbox Code Playgroud)
一切都很完美,但是!我想load_parent用RSpec 测试该方法: …