Rails Routing (root :to => …)

元气小坏坏 提交于 2019-12-02 16:33:14

Had this same problem and this worked for me:

root :to => "pages#show", :id => '1'
Brian Petro

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

root 'controller#action'

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

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

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

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

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

This will point the root route to /blogs/1

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

Cheers

Try:

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

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

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

Hope that helps.

Abdalhaqq Muhammad Saih

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

root 'pages#show', {id: 1}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!