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?

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


Need Your Help

Understanding Backbone and Marionette View lifecycle

javascript backbone.js marionette lifecycle backbone-views

I'm new to this world and I need to understand some of the concepts of Backbone and Marionette. Here I'm trying to explain some of the concepts I'm learning. It would be great to having some feedba...

Linq query to group by field1, count field2 and filter by count between values of joined collection

c# linq linq-to-objects

I'm having trouble with getting a my linq query correct. I've been resisting doing this with foreach loops because I'm trying to better understand linq.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.