views:

56

answers:

2

This is an excerpt from my config/routes.rb file:

resources :accounts do |account|

      account.resource :profile, :except => [:new, :create, :destroy]

      account.resources :posts,
                        :collection => { :fragment => :get },
                        :has_many => [:comments, :likes]

      # even more code

end

I would like that each nested resource to be loaded from from the account namespace such as Account::PostsController instead of PostsController.

Using resources :accounts, :namespace => 'account' tries to load AccountPostsController.

Trying to nest the structure doesn't really work all that well:

map.namespace :account do |account|
..
end

The previous code will load the files from the locations I want, however it does add the namespace to the url and the generated paths so I'll have methods such as account_account_posts_url and similar paths.

Another alternative is to use something like:

account.resource :profile, :controller => 'account/profile'

I really don't like this as it involves both code duplication and forces me to remove some of the rails magic helpers.

Any thoughts and suggestions?

A: 

How about something like this?

map.resources :accounts do |accounts|
  accounts.with_options(:namespace => "account") do |account|
    account.resource :profile, :except => [:new, :create, :destroy]
    ...
  end
end

I haven't tried this so have no idea if it will work, but it's a start. See Rails Routing for more detailed info and options on what can be done with Rails Routes.

After Down vote

So I ran some test. Changing my routes.rb and running rake routes and came up with the following (pretty close to what I had to begin with):

map.resources :accounts do |accounts|
  accounts.namespace :account do |account|
    account.resource :profile, :except => [:new, :create, :destroy]
  end
end

This gets you what you want. The correct url and pointing to account/... controller.

Tony Fontenot
Many thanks. I don't know who modded you down, but you definitely get a +1 from me.
vise
No worries. Glad you got the route definitions you needed. The down mark was more than likely due to the `I haven't tried this so have no idea if it will work` statement.
Tony Fontenot
A: 

So what's specifically wrong with namespacing? I think this is what you're trying to do:

map.namespace :account do |account|
  account.resource :profile
end

This will try to load the controller at app/controllers/account/profiles_controller.rb and will generate routes such as account_profile_path.

Updated based on comment:

map.resources :accounts do |account| 
 account.resource :profile
end

Will give you /accounts/22/profile.

Ryan Bigg
Thank you for your reply. Sorry for not being to clear, but what I'm after is a route that looks like this:/accounts/22/profileThe above code would generate account/profile.
vise