4


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?

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

2 Comments

Also add :as => :my_page at the end, and you get a my_page_path helper to use in your views.
A little late, but path helper generates path and not a url. So my_page_path will return "/my_page_here".
4

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

2 Comments

match is old and should not be used!
you can use :via => [:get] to make the match respond only to GET requests. For normal static pages, match works fine. Rails is built in such a way that the app makers doesnt need to know the HTTP verbs - so for most content displaying pages rails offloads the burden of http using match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.