views:

26

answers:

3

I'll preface this with the fact that I'm new to RoR. I have a class that resides in a nested module, ::Physical::Users::User. The code is organized into paths such as app\controllers\physical\users.

My problem comes when I try to use form_for like this:

<% form_for @user do |f| %>
  ...
<% end %>

I get the error ActionView::TemplateError undefined method 'physical_users_users_path'

I understand that this has to do with rails expecting routes that don't match mine. In order to make my app work so that the resource :users goes directly to the correct directory (so that I don't have to use http:\myurl.com\physical\users\users\1', I put the following in my routes.rb file:

map.resources :users, :controller => 'physical/users/users'

When I run ">rake routes" I get the following:

    users GET    /users(.:format)                   {:controller=>"physical/users/users", :action=>"index"}
          POST   /users(.:format)                   {:controller=>"physical/users/users", :action=>"create"}
 new_user GET    /users/new(.:format)               {:controller=>"physical/users/users", :action=>"new"}
edit_user GET    /users/:id/edit(.:format)          {:controller=>"physical/users/users", :action=>"edit"}
     user GET    /users/:id(.:format)               {:controller=>"physical/users/users", :action=>"show"}
          PUT    /users/:id(.:format)               {:controller=>"physical/users/users", :action=>"update"}
          DELETE /users/:id(.:format)               {:controller=>"physical/users/users", :action=>"destroy"}
                 /:controller/:action/:id           
                 /:controller/:action/:id(.:format) 

Is there a way to maintain the shorter url to my user resource and get form_for to look for the correct route?

Again, I'm new, so if there's anything I'm missing in this explanation, let me know!

--

In response to replies below, the controller resides at app/controllers/physical/users/users_controller.rb.

Changing the routing to use namespaces like:

map.namespace :physical do |physical|
  physical.namespace :users do |users|
    users.resources :users
  end
end

generates routes that are consistent with what form_for is looking for, but then I lose the ability to use the URL http:\mysite.com\users\1.

Thanks for the help so far!

+1  A: 

In routes, you must represent modules with namespaces:

namespace :physical do
  namespace :users do
    map.resources :users
  end
end
sespindola
I tried this and script/server complained. Did you mean: map.namespace :physical do |physical| physical.namespace :users do |users| users.resources :users end endThis works in the sense that it creates the correct paths, but how would I then go about making sure that the path /users/:id goes to physical/users/users_controller?
Bryan Marble
+1  A: 

Agree with @sespindola, but with the controller at app\controllers\physical\users --- I think it's a namespace issue

I think this may work for you:

map.resources :users, :path_prefix => ':physical'
Jesse Wolgamott
If that's the case your answer is right.I Thought he had the controller on app/controllers/physical/users/users.
sespindola
Right! I'll ask him to confirm
Jesse Wolgamott
A: 

The answer that I've found to work is actually a combination of Jesse Wolgamott and sespindola's answers. I've voted them both up. I placed the following in my routes.rb file:

map.namespace :physical do |physical|
  physical.namespace :users do |users|
    users.resources :users
  end
end

map.resources :users, :controller => "physical/users/users"

If this seems kludgy and not really the best way to do it, leave a comment. Thanks all for the help!

Bryan Marble
You should not need both the users.resource :users AND the map.resources :users -- only one should be present in your routes files. I recommend you steer away from a complicated scheme and just keep it simple. If you have a controller resources (as a first class citizen in your system), just have a map.resources :users.
Jesse Wolgamott