Rails 3 - changing index - ruby-on-rails

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.

Related

Getting error while using RefineryCMS Blog route

I instructed config/routes.rb to use Refinery Blog as a root directory:
root :to => "refinery/blog/posts#index"
mount Refinery::Core::Engine, :at => '/'
In a app/view/layouts/_header.html.slim I'm trying to use blog_root route. For example:
= link_to (image_tag "/logo.gif"), blog_root, class: "brand"
The route is listed when I issue rake routes:
blog_root /blog(.:format) refinery/blog/posts#index
But nothing shows up, the system gives an error:
undefined local variable or method `blog_root' for
#<#<Class:0x00000005e62f80>:0x007fd7241d94c8>
Also, I tryed blog_root_path, but it didn't work either.
Anything I can do in this situation? Thanks a lot!
This question/answer pair was helpful.
I looked inside config/routes.rb of main app, and in comments it was writted that 'We ask that you don't use the :as option here, as Refinery relies on it being the default of "refinery"'.
So, the working route is refinery.blog_root_path.

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

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}

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.

A very basic issue with routes in ruby

I am new to ruby and while creating a sample application found out an issue that whenever I go to http://127.0.0.1:3000/people/index by default show action is executed and index is taken as a parameter. This is server log:
Started GET "/people/index" for
127.0.0.1 at 2010-12-23 18:43:01 +0500 Processing by PeopleController#show as
HTML Parameters: {"id"=>"index"}
I have this in my route file:
root :to => "people#index"
resources :people
match ':controller(/:action(/:id(.:format)))'
What is going on here and how can I fix the issue?
The route
resources :people
creates "sub"-routes
get '/people' => 'people#index'
get '/people/new' => 'people#new'
post '/people' => 'people#create'
get '/people/:id' => 'people#show'
get '/people/:id/edit' => 'people#edit'
put '/people/:id' => 'people#update'
delete '/people/:id' => 'people#destroy'
Actually, all of these sub-routes include (.:format) at the end of the recognized path.
The path /people/index would be recognized by the route /people/:id, mapping to the action #show.
The path /people would be recognized by the route /people, mapping to the action #index.
Use the URL helpers people_path and people_url for the /people route.
To get Rails to travel backward in time to before it espoused REST and to understand /people/index, do this:
resources :people do
get :index => 'people#index'
end
You might want to watch this Railscast episode.
A couple things to keep in mind when working with your routes:
rake routes dumps the map of URLs to your controllers
When providing backwards compatibility, redirect the user to the correct path
I personally have yet to upgrade my app to Rails 3, and I'll be dragging my feet until I really need to do it (just got it out the door not too long ago). In Rails 2.x you had resource routes, but if you kept the default controller/action/id route it would fall through and resolve. It appears that is no longer the case in Rails 3. Essentially your resource routes handle all URLs in that resource namespace (/people in your case).
To provide backwards compatibility, I would add a redirect route to resolve that incompatibility.
match "/people/index", :to => redirect("/people")
The main reason for that is to prevent users from saving an incorrect URL for their personal links--while allowing legacy users to still be able to get where they meant to go.
Edit: New answer, removed pointing out the typo in the question.

Resources