Rails Routing (root :to => ...) - ruby-on-rails

I know how to set the routes root of my rails app to a controller and an action.
But how to add an id?
/pages/show/1 should be the root.
How do I set this?

Had this same problem and this worked for me:
root :to => "pages#show", :id => '1'

As of Rails 4.0, you can declare the root route like this:
root 'controller#action'

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

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.

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.

Use Rails 5.1
Add this to the config/routes.rb
root 'pages#show', {id: 1}

Related

Multiple 'root to' routes in rails 4.0

I am trying to get rails to go to different controller#action according to the subdomain, and this is what I have so far in routes.rb
Petworkslabs::Application.routes.draw do
get '/', to: 'custom#show', constraints: {subdomain: '/.+/'}, as: 'custom_root'
get '/', to: "welcome#home", as: 'default_root'
end
rake shows the correct routes I want it to take
rake routes
Prefix Verb URI Pattern Controller#Action
custom_root GET / custom#show {:subdomain=>"/.+/"}
default_root GET / welcome#home
But for some reason, I can't get requests like abc.localhost:3000 to hit the custom controller. It always routes it to welcome#home. Any ideas? I am fairly new to rails, so any tips about general debugging would also be appreciated.
EDIT: I stepped through the code using the debugger and this is what I found
(rdb:32) request.domain
"abc.localhost"
(rdb:32) request.subdomain
""
(rdb:32) request.subdomain.present?
false
Looks like for some reason rails thinks that the subdomain is not present, even though its there. I wonder if its because I am doing this localhost.
Updated Answer:
Worked for me on Rails 3 & 4:
get '/' => 'custom#show', :constraints => { :subdomain => /.+/ }
root :to => "welcome#home"
#manishie's answer is right, but you'll still likely have issues in your devo environment if you're using localhost. To fix it add the following line to config/environments/development.rb:
config.action_dispatch.tld_length = 0
and then use #manishie's answer in routes.rb:
get '/' => 'custom#show', :constraints => { :subdomain => /.+/ }
root :to => "welcome#home"
The issue is that tld_length defaults to 1 and there's no domain extension when you're using localhost so rails fails to pickup the subdomain. pixeltrix explains it really well here: https://github.com/rails/rails/issues/12438
For some reason request.subdomain was not getting populated at all (I suspect this is because I have doing this on localhost, I have opened a bug here https://github.com/rails/rails/issues/12438). This was causing the regex match in routes.rb to fail. I ended up creating a custom matches? method for Subdomain which looks something like this
class Subdomain
def self.matches?(request)
request.domain.split('.').size>1 && request.subdomain != "www"
end
end
and hooking that up in routes.rb
constraints(Subdomain) do
get '/', to: "custom#home", as: 'custom_root'
end
this seems to work.
EDIT: More information in the github issues page https://github.com/rails/rails/issues/12438

Custom url in ruby on rails

I know rails uses the controller action style urls like www.myapp.com/home/index for example
I would like to have a url like this on my rails app, www.myapp.com/my_page_here is this possible and if so how would I go about this?
You just use a get outside of any resources or namespace block in your routes.rb file:
get 'my_page_here ', :to => 'home#index'
Assuming you are using Rails 3+, do NOT use match. It can be dangerous, because if a page accepts data from a form, it should take POST requests. match would allow GET requests on an action with side-effects - which is NOT good.
Always use get, put, post or these variants where possible.
To get a path helper, try:
get 'my_page_here ', :to => 'home#index', :as => :my_page
That way, in your views, my_page_path will equal http://{domain}/my_page_here
you just need to make a routing rule to match that url
in this case it will be something like
match 'my_page_here' => 'your_controller#your_action'
your controller and action will specify the behavior of that page
so you could do
match 'my_page_here' => 'home#index'
or
get 'my_page_here', :to => 'home#index'
as suggested in other responses.
for index action in home controller if you have such a controller
see http://guides.rubyonrails.org/routing.html for more details
also see Ruby on Rails Routes - difference between get and match

How can I rename a Rails controller with a route?

I have a controller in a Rails 3 app named "my_store." I would like to be able to use this controller as is, except replacing "my_store" in all the URL's with another name. I do not want to rename the controller file, and all the references to it. Is there a clean way to do this with just a routing statement?
If you use RESTful routes:
resources :another_name, :controller => "my_store"
Otherwise:
match "another_name" => "my_store"
If your routes are RESTful, this is pretty easy.
resources :photos, :controller => "images"
You can see how to do this and other helpful Rails routing information in the Rails routing guide.
Update, the other guys are correct, to replace all references you would change the resources name and corresponding controller in routes.rb! My answer is only good to set a specific route.
Yup, you would do this in your routes.rb using the :as option to specify
example:
match 'exit' => 'sessions#destroy', :as => :logout
source

Rails 3 - changing index

I'm using Rails 3 and have the feeling that the syntax for changing the route for the index (http://localhost:3000) is different than from the former versions.
I'd like to open the dynamic index page (index.html.erb) of the employees-controller (which can be right now opened with localhost:3000/employees) as the default page (localhost:3000). I thought it's quite easy, because in the routes it's written:
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => "welcome#index"
So that's what I actually did: I deleted public/index.html and set root :to => "employees#index".
But when I open the server and open localhost:3000, it's still opening the "welcome abroad!"-page. Pretty weird!
So I googled the problem and found answers which said, I should write this into my routes-file:
map.root :controller => 'employees', :action => 'index'
Same here - I also still get the "welcome abroad!"-page and the rails-shell says "undefined local variable or method 'map'". (I think this is the old syntax for Rails 2...?)
match "/" => "employees#index" says routing error: No route matches "/"
So what did I do wrong? How can I solve this problem?
Why you use "map" in Rails 3? Should be:
root :to => "employees#index"
I think problem is of cookies. please clear the cookies and the refresh the page.

Rails routing to root

When setting up the root route, is there any compelling reason to choose this syntax:
map.root :controller => "login", :action => 'show'
over this syntax:
match "/" => "login#show"
The second syntax will allow you to use the :constraints option, where the first wont. Is there any reason to use the first option?
When you use root :to rails 3 automatically creates the helper methods root_url and root_path for referencing your application root. These methods are often used in gems to reference your applications root and I'm not actually sure where these would point or if they would even work if you don't specify anything (never tried it). Plus it's the "rails way" of doing things so it's usually best to follow unless you have a really good reason.
I believe root routes should be set up as follows:
root :to => "Something#index"
The methods you suggested sound like they may cause conflicts later on down the road.
In Rails 4, here's a quicker code you can use:
root 'login#new_session'
You can substitute new_session with show/index/etc, just make sure to define it in your login controller.
I think the following two are the same:
root :to => 'login#show'
match '/' => 'login#show', :as => :root
Just like other paths, if you want a root_path, then for the match '/', you have to specify it by yourself.
So I think they just do the same thing (routing you to login#show if the path is /), but the first one would have more semantic meaning.

Resources