Gagan
Gagan

Reputation: 573

Remove controller_name from URL in Rails

My Url is http://www.example.com/players/playersdetail/100006 i have a players controller. I need to remove or hide controller and method name (players/playersdetail) from url and i want name in place of id(100006)

  1. Ex- http://www.example.com/gagan-gupta
  2. Ex- http://www.example.com/gagan-gupta/about
  3. Ex- https://www.facebook.com/gagan-gupta/friends

How to achieve in Ruby On Rails?

Routes is

Something::Application.routes.draw do
      get "players/search"
      post "players/search"
      get "players/playerslist"
      get "players/playersdetail"
      get "players/list"
      get "players/followers"
      get "players/following"    
  end

Upvotes: 0

Views: 952

Answers (2)

Sumit Munot
Sumit Munot

Reputation: 3868

Your routes.rb something like this:

resources :players

which produces routes of the form /entries/24225252/foo.

There exists a :path argument that allows you to use something besides the default name entries. For example:

resources :players, :path => 'my-cool-path'

will produce routes of the form /my-cool-path/2012/05/10/foo.

But, if we pass an empty string to :path, we see the behaviour you're looking for:

resources :players, :path => ''

will produce routes of the form /2012/05/10/foo.

Also another option:

  get ':year/:month/:day/:title' => 'some_controller#show', :as => 'players'
  put ':year/:month/:day/:title/edit' => 'some_controller#edit', :as => 'edit_players'
  delete ':year/:month/:day/:title' => 'some_controller#destroy', :as => 'destroy_players'

Upvotes: 1

mahemoff
mahemoff

Reputation: 46369

Your::Application.routes.draw do
  scope ":name" do
    get '', to: 'players#show'
    get :about, to 'players#about'
    get :friends, to 'players#friends
  end
end

Reference

Upvotes: 1

Related Questions