[ACCEPTED]-Rails Routing (root :to => ...)-url-routing

Accepted answer
Score: 80

Had this same problem and this worked for 1 me:

root :to => "pages#show", :id => '1'
Score: 32

As of Rails 4.0, you can declare the root route like 1 this:

root 'controller#action'
Score: 6

I'm using Rails 5.1 to point the home page 5 to a specific blog. In config/routes.rb 4 I have ...

root 'blogs#show', {id: 1}

This will point the root route 3 to /blogs/1

I'm doing this on a blog site 2 I'm building. The first blog will be the 1 main site blog as well as the homepage.


Score: 4

Matthew's solution works, but I think it 8 is more readable to fetch the object. For 7 example, let's say you want to root to the 6 Page#show action for the page with the name "landing". This 5 is a bit more readable:

root :to => "pages#show", :id => Page.find_by_name("landing").id

From a performance 4 perspective, this solution is worse because 3 it requires an additional database query, but 2 this solution is more readable if performance 1 is not a high priority.

Score: 3


 match 'pages/show/:id' => 'pages#show', :as => :root

In Rails console. rake routes | grep root, should show something 1 like:

root     /pages/show/:id(.:format)      {:controller=>"pages", :action=>"show"}

Hope that helps.

Score: 2

Use Rails 5.1 Add this to the config/routes.rb

root 'pages#show', {id: 1}


More Related questions