Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom url in ruby on rails


I know rails uses the controller action style urls like www.myapp.com/home/index for example
I would like to have a url like this on my rails app, www.myapp.com/my_page_here is this possible and if so how would I go about this?

like image 943
Francois Avatar asked Nov 01 '25 06:11

Francois


1 Answers

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

like image 84
ronalchn Avatar answered Nov 03 '25 23:11

ronalchn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!