简体   繁体   English

从Rails中的URL中删除controller_name

[英]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)

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

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::Application.routes.draw do
  scope ":name" do
    get '', to: 'players#show'
    get :about, to 'players#about'
    get :friends, to 'players#friends
  end
end

Reference 参考

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.

相关问题 从Rails 3中的URL中删除controller_name并使用自定义字符串而不是id - Remove controller_name from URL in Rails 3 and use custom string instead of id 模型方法中的Rails4 Access controller_name - Rails4 Access controller_name from model method 在rails日志中将controller_name处理为* / *的含义 - Meaning of processing controller_name as */* in rails logs Rails:一些索引路由显示“ / controller_name / index”,其他仅显示“ / controller_name” - Rails: Some index routes are showing “/controller_name/index” others are just showing “/controller_name” 如何在Ruby on Rails中从views / controller_name / page1.html.erb链接到公用文件夹中的项目? - How do I link to an item in the public folder from views/controller_name/page1.html.erb in Ruby on Rails? 在rails 3.1.0中,params [:controller_name] [:field1]和params [:field1]是否相同? - Are params[:controller_name][:field1] and params[:field1] the same in rails 3.1.0? Rails:从URL删除控制器 - Rails: Remove Controller from URL 如何获得父母的controller_name? - How to get parent's controller_name? 如何从其父级获取子类控制器的controller_name? - How to I get the controller_name of a subclassed controller from its parent? 没有匹配的路由{:action =>“ show”,:controller =>“ controller_name”} - No route matches {:action=>“show”, :controller=>“controller_name”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM