[英]Remove controller_name from URL in Rails
My Url is http://www.example.com/players/playersdetail/100006 i have a players controller. 我的网址是http://www.example.com/players/playersdetail/100006我有一个玩家控制器。 I need to remove or hide controller and method name (players/playersdetail) from url and i want name in place of id(100006) 我需要从url中删除或隐藏控制器和方法名称(players / playersdetail),我想用名字代替id(100006)
How to achieve in Ruby On Rails? 如何在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
Your routes.rb something like this: 你的routes.rb是这样的:
resources :players
which produces routes of the form /entries/24225252/foo
. 它产生form /entries/24225252/foo
路线。
There exists a :path argument that allows you to use something besides the default name entries. 存在一个:path参数,允许您使用除默认名称条目之外的内容。 For example: 例如:
resources :players, :path => 'my-cool-path'
will produce routes of the form /my-cool-path/2012/05/10/foo.
将生成/my-cool-path/2012/05/10/foo.
形式的路线/my-cool-path/2012/05/10/foo.
But, if we pass an empty string to :path, we see the behaviour you're looking for: 但是,如果我们将空字符串传递给:path,我们会看到您正在寻找的行为:
resources :players, :path => ''
will produce routes of the form /2012/05/10/foo.
将生成形式为/2012/05/10/foo.
路线/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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.