Rails routing to root - ruby-on-rails

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.

Related

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 how to rewrite this old route map.connect?

How do I rewrite this old route Rails 1.2.6 to Rails 3? :
# Allow downloading Web Service WSDL as a file with an extension
# instead of a file named 'wsdl'
map.connect ':controller/service.wsdl', :action => 'wsdl'
I can´t see how I should use match route etc.
I have used:
match ':controller/service.wsdl', :action => 'wsdl'
But I dont think it is working correct
Try this:
match '/controller/service.wsdl' => 'controller#service.wsdl', :as => :wsdl
I'm guessing that your controller is not named controller. If it is, I'd rename it and change the above route as well.
I haven't found a good solution to converting Rails 2 parameterized :controller and :action generic routes to the more explicit Rails 3+ format. What I've had to do is go through every permutation in my app and add an explicit route for everything I needed to support. For example, in your case, if you had 3 controllers that supported the wsdl action, I'd add a new route for each using either match or get.
Here's what it might look like, assuming you had a foo_controller, bar_controller, and a blah_controller that all support the wsdl action:
get '/foo/service.wsdl' :to => 'foo#wsdl'
get '/bar/service.wsdl' :to => 'bar#wsdl'
get '/blah/service.wsdl' :to => 'blah#wsdl'
This gets even more fun when you need to support every action on a controller when they use :action.
If anyone has a better method, I'm open (and eager) to hear of a better way.

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 (root :to => ...)

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}

Question about routes.rb

Rails newbie here.
Can anyone please explain the difference to me between the following lines of code:
match '/' => 'posts#index'
and
match '/' => 'posts#index', :as => 'posts'
The reason I'm asking is because when I use the latter code, I cannot create new posts :|
The latter is creating a named route. It creates a helper that you can call from your views, in this case, posts_path & posts_url.
That being said, I'm not sure how you are able to create new posts with either of those as you are not defining the posts#new or posts#create. Is there more to your routes file than these? Also, I'm not sure if it's a requirement or not, but you should pass your :as option as a symbol, so :as => :posts.
For reference, you can run rake routes from console and see a list of all the routes that are defined in your application. You'll also see how they are named—that's the column all the way to the right—which you can then append _path or _url to.

Resources