Custom url in ruby on rails

允我心安 提交于 2019-12-01 20:15:49

You just use a get outside of any resources or namespace block in your routes.rb file:

get 'my_page_here ', :to => 'home#index'

Assuming you are using Rails 3+, do NOT use match. It can be dangerous, because if a page accepts data from a form, it should take POST requests. match would allow GET requests on an action with side-effects - which is NOT good.

Always use get, put, post or these variants where possible.

To get a path helper, try:

get 'my_page_here ', :to => 'home#index', :as => :my_page

That way, in your views, my_page_path will equal http://{domain}/my_page_here

Nick Ginanto

you just need to make a routing rule to match that url in this case it will be something like

match 'my_page_here' => 'your_controller#your_action'

your controller and action will specify the behavior of that page

so you could do

match 'my_page_here' => 'home#index'

or

get 'my_page_here', :to => 'home#index'

as suggested in other responses.

for index action in home controller if you have such a controller

see http://guides.rubyonrails.org/routing.html for more details

also see Ruby on Rails Routes - difference between get and match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!