3

I have 2 namespaces, api and v1 I have accounts and users as resources.

I want to map the routing as follows for all my resources:

/api/v1/:account_id/:resource/:id

i.e: /api/v1/1/users/2

In the example 1 stands for account id and 2 stands for user id.

How do I accomplish this?

3
  • Welcome to Stack Overflow. Your question is a bit obscure, it's difficult to understand what you're asking. I suggest that you edit your question to make it more understandable Commented May 17, 2013 at 11:33
  • What? I think it's pretty straight forward. I even gave an example. What is it that you don't understand? Commented May 17, 2013 at 12:12
  • I apologise, I stand corrected. The lack of formatting made me jump the gun a bit Commented May 17, 2013 at 14:04

1 Answer 1

7

This does away with namespaces, such that you don't need to append API::V1:: to each controller, or bury view files in subdirectories. The following uses normal, top-level controllers and views:

scope '/api/v1/:id', :as => 'account' do
  resources :users
end

If you want to keep all the namespace structure stuff, do this:

namespace 'api' do
  namespace 'v1' do
    scope '/:id', :as => 'account' do
      resources :users
    end
  end
end
Sign up to request clarification or add additional context in comments.

6 Comments

Something is happening, but now I get a uninitialized constant UsersController. The idea was for the directory structre: app - ws - v1 - users_controller.rb Doesn't seem to work like that. Is there any way I could use namespace instead of scope? Noob questions, sorry, just started with rails and I'm forced by the Client to do it like this.
had to add... to: 'api/v1/users'
Answer updated. Also, I am going off of your example. You need to replace resources :users with the resources you actually have.
Only new to rails, not programming in general :)
One more question, sorry, I'm getting a "Template is missing", obviously I don't have any templates, but I just want to return .json Tried respond_to :json, doesn't seem to do the trick, am I missing something?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.