Rails Routes - show action as root

I want to make the show action one of my root controllers. I can easily do this:

map.root :controller => 'articles', :action => 'index'

When I switch to localhost: 3000 / it lists all the articles - that's great! However, I want this URL to be as follows:

localhost:3000/1

To display the article with ID 1. Having changed the route, I would have thought that I should do the following:

map.root :controller => 'articles', :action => 'show'

But that does not work. Instead, he searches for a controller called 1- which does not exist.

How should I do it?

Thank!

+3
source share
4 answers

Try it on routes.rb

map.connect ':id', :controller => 'articles', :action => 'show'

, - , . I.e., routes.rb, ( ):

# low-priorty article show route
map.connect ':id', :controller => 'articles', :action => 'show'

# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing or commenting them out if you're using named routes and resources.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
+6

. .

map.connect '/:id', :controller => 'articles', :action => 'show'

, , (http://api.rubyonrails.org/classes/ActionController/Resources.html#M000522) .

+5

Rails 3 . - :

match ':id', :controller => 'articles', :action => 'show'

, .

+3

resource :articles, :path => '/'

root

+2

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


All Articles