Rails 3 URL without Controller Name

Suppose I have a blog with Rails 3 on my website, and that will be the only thing I have. I would like to use Rails to implement it, but I don't like the Rails URLs. I need the following URLs:

example.com/2012/05/10/foo

I don't need something like this that I know how to do this (with to_param):

example.com/entries/2012/05/10/foo

I still want to use helpers like

 new_entry_path(@entry) # -> example.com/new entry_path(@entry) # -> example.com/2012/05/10/foo edit_entry_path(@entry) # -> example.com/2012/05/10/foo/edit destroy_entry_path(@entry) form_for(@entry) link_to(@entry.title, @entry) 

etc. Then I will have comments and you want to make them available as your own resources, for example

example.com/2012/05/10/foo/comments/5

and these URLs should also be accessible to regular helpers:

 edit_entry_comment_path(@entry, @comment) # -> example.com/2012/05/10/foo/comments/5/edit 

or something like that.

So, is it possible to get URLs without a controller name and use url helper methods? Just rewriting to_param will always just change the part after the controller name in the url. It would be very helpful to get some sample code.

+6
source share
1 answer

Your routes.rb probably has a line something like this:

 resources :entries 

which creates routes of the form /entries/2012/05/10/foo .


There is an argument :path , which allows you to use something other than the default name entries . For instance:

 resources :entries, :path => 'my-cool-path' 

will create routes of the form /my-cool-path/2012/05/10/foo .


But if you pass an empty string to :path , we will see the behavior you are looking for:

 resources :entries, :path => '' 

will create routes of the form /2012/05/10/foo .

+13
source

Source: https://habr.com/ru/post/919360/


All Articles