简体   繁体   中英

Creating an API namespaced controller in Rails

I'm trying to test my namespaced controller and not having much luck. I have the following route setup:

namespace :api do
  get 'organization/:id/questions/:number', controller: 'questions', action: 'index', as: 'organization_questions'
end

which produces the following route:

api_organization_questions GET    /api/organization/:id/questions/:number(.:format)      {:controller=>"api/questions", :action=>"index"}

that route works, and I'm able to successfully make a request to it with the following url: http://localhost:3000/api/organization/1/questions/1234567890

However when I try to make a get request to it in my unit test I get the following error:

No route matches {:controller=>"api/questions", :action=>"/api/organization/1/questions/1234567890"}

my get request looks like this:

get api_organization_questions_path(@organization.id, '1234567890')

Not sure what I'm doing wrong!?

What are you using for testing ? RSpec? The first parameter for the get method is the action. The code below should make the request you want:

describe Api::QuestionsController do
  it "should do something" do
    get :index, :id => @organization.id, :number => '1234567890'
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM