0

我的网址是 http://www.example.com/players/playersdetail/100006 我有一个播放器控制器。我需要从 url 中删除或隐藏控制器和方法名称(players/playersdetail),我想用名称代替 id(100006)

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

如何在 Ruby On Rails 中实现?

路线是

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
4

2 回答 2

1

你的 routes.rb 是这样的:

resources :players

产生的路线form /entries/24225252/foo

存在一个 :path 参数,允许您使用除默认名称条目之外的其他内容。例如:

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

将产生形式的路线/my-cool-path/2012/05/10/foo.

但是,如果我们将一个空字符串传递给 :path,我们会看到您正在寻找的行为:

resources :players, :path => ''

将产生形式的路线/2012/05/10/foo.

还有另一种选择:

  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'
于 2013-05-04T12:34:46.000 回答
1
Your::Application.routes.draw do
  scope ":name" do
    get '', to: 'players#show'
    get :about, to 'players#about'
    get :friends, to 'players#friends
  end
end

参考

于 2013-05-04T11:46:42.970 回答