自定义URL在轨道上的红宝石

Fra*_*ois 3 ruby url ruby-on-rails url-routing


我知道rails使用控制器动作样式的网址www.myapp.com/home/index,例如
我想在我的rails应用程序上有这样的网址,www.myapp.com/my_page_here这是否可能,如果是这样,我该怎么做呢?

ron*_*chn 6

您只需在文件中使用getany resourcesnamespaceblock 外部routes.rb:

get 'my_page_here ', :to => 'home#index'
Run Code Online (Sandbox Code Playgroud)

假设您使用的是Rails 3+,请不要使用match.它可能很危险,因为如果页面接受表单中的数据,它应该接受POST请求.match允许对带有副作用的动作发出GET请求 - 这不是好事.

始终使用get,put,post或这些变体在可能的情况.

要获取路径助手,请尝试:

get 'my_page_here ', :to => 'home#index', :as => :my_page
Run Code Online (Sandbox Code Playgroud)

那样,在你的观点中,my_page_path将是平等的http://{domain}/my_page_here