Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.2rails-routing

Rails routing like github


I am using Rails 3.2

I want to have routing pretty much exactly like github, so:

root/(username)
root/(username)/(projectname)
root/(username)/(projectname)/issus

etc.

I am trying something like this:

resources :publishers do
  resources :magazines do
    resources :photos
  end
end 

But that gives routes like this:

/publishers/1/magazines/2/photos/3

A project I am looking at does the following which seems to work but does not seem to be for me.

resources :projects, :constraints => { :id => /[^\/]+/ }, :except => [:new, :create, :index], :path => "/" do
member do
  get "team"
  get "wall"
  get "graph"
  get "files"
end

resources :wikis, :only => [:show, :edit, :destroy, :create] do
  member do
    get "history"        
  end
end

Solution

  • If you want to get rid of the id number (which is rails default) and use a name I suggest the FriendlyId gem.

    watch this railscast http://railscasts.com/episodes/314-pretty-urls-with-friendlyid

    and here is the github page https://github.com/norman/friendly_id

    EDIT

    This is the article I was looking for, I forgot I bookmarked it months ago. http://jasoncodes.com/posts/rails-3-nested-resource-slugs