2015-11-24 4 views
6

मेरे पास एक अजीब स्थिति है। मैं अपने 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 

तो, यह मेरे सहायकों vote_up_question और vote_up_answer 'पोस्ट' के माध्यम से देता है:

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 

मेरे 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 

सब कुछ सही काम करता है, लेकिन! मैं RSpec के साथ कि load_parent विधि का परीक्षण करना चाहते:

RSpec.describe VotesController, type: :controller do 
    let(:question) {create(:question)} 
    let(:answer) {create(:answer, user_id: user, question_id: question)} 
    let(:user) {create(:user)} 
    before(:each) do |example| 
    sign_in user if example.metadata[:sign_in] 
    request.env["HTTP_REFERER"] = question_path(question) if example.metadata[:redirect_back] 
    end 


    describe 'POST #vote_up', sign_in: true, redirect_back: true do 
    it 'should assign question to @parent' do 
     post vote_up_question_path(question) 
     expect(assigns(:parent)).to eq 'question' 
    end 
    end 
end 

और यहाँ एक समस्या है:

ActionController::UrlGenerationError: 
     No route matches {:action=>"https://stackoverflow.com/questions/1/vote_up", :controller=>"votes"} 

क्या गलत है? मैं भी post :vote_up, question_id: question की तरह एक स्पष्ट मार्ग बनाने के लिए, अलग अलग तरीकों से कोशिश की, लेकिन यह काम नहीं करता

+1

आप '' रेक routes'' उत्पादन दिखाने सकते हैं? –

+0

इस प्रश्न के लिए जोड़ा गया हिस्सा। –

+2

क्या आपने 'post: vote_up, id: question.id' की कोशिश की है? –

उत्तर

0

प्रयास करें:

let!(:question) {create(:question)} 
let!(:user) {create(:user)} 
let!(:answer) {create(:answer, user_id: user, question_id: question)} 

देना आलसी से मूल्यांकन किया है इसलिए यदि यह इस ब्लॉक में यह हो सकता है में लागू नहीं किया गया है अपरिभाषित होना

तब कार्य करें:

describe 'POST #vote_up', sign_in: true, redirect_back: true do 
it 'should assign question to @parent' do 
    post :vote_up, id: question.id 
    expect(assigns(:parent)).to eq 'question' 
end 

अंत

संबंधित मुद्दे

 संबंधित मुद्दे