Mathieu Mahé
Mathieu Mahé

Reputation: 2746

Route with empty path and helper

I'm trying to implement a "à la github" url like "github.com/josevalim/inherited_resources".

I achieved my goal with this code :

 resources :users, :path => '', only: [] do
   resources :projects, :path => '', only: [:show]
 end

But now, I can't find a way to redirect properly to this route.

When I run a rake routes in the console, all I have is

GET      /:user_id/:id(.:format)        projects#show

and no name to find a helper method like my old users_project_path.

Have I miss something ? Thanks!

Upvotes: 3

Views: 1383

Answers (2)

Bart Jedrocha
Bart Jedrocha

Reputation: 11570

I recently did something similar, try the following

scope '/:user_id', as: :user do
  resources :projects, path: '', only: [:show]
end

Running rake routes should then give you

user_project GET    /:user_id/:id(.:format)        projects#show

and this will give you the proper user_project_path(:user_id, :id) route helper.

Upvotes: 3

ppascualv
ppascualv

Reputation: 1137

You're deleting the path for this reason you can get route_path, use as to establish a route name.

resources :users, :path => '', only: [] do
   get :projects, "/:user_id/:id", :to => "project#show", :as => :project_user
 end

OR if you don't want use that you can use this code for generating path:

url_for([@user, @project])

Upvotes: 0

Related Questions