[ACCEPTED]-Rails Routing (root :to => ...)-url-routing
Had this same problem and this worked for 1 me:
root :to => "pages#show", :id => '1'
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.
Cheers
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.
Try:
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.
Use Rails 5.1 Add this to the config/routes.rb
root 'pages#show', {id: 1}
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.