Multiple 'root to' routes in rails 4.0 - ruby-on-rails

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

Related

Rails routes redirection for subdomains

We can't change the server configuration files, so we need to do our redirections at the rails level.
I have no problem with path redirections to external sites, like:
match "/meow" => redirect("http://meow.com/")
The issue is with the subdomains. I need to redirect for example:
http://my.example.com => http://example.com
How can this be done using routes.rb?
According to #cfernandezlinux's amazing answer, here's the same in Rails 4/Ruby 2 syntax:
constraints subdomain: "meow" do
get "/" => redirect { |params| "http://www.externalurl.com" }
end
match in routes.rb is not allowed in Rails 4.0 anymore. You have to use explicitly get, post, etc.
hashrocket syntax (=>) is for old Ruby, now in Ruby 2.0 we use param: 'value' syntax
I ended up doing something like this:
constraints :subdomain => "meow" do
match "/" => redirect { |params| "http://www.externalurl.com" }
end
If you don't want to hard-code the URL (so, for example, you can test/use this locally), you could do:
constraints subdomain: 'subdomain_from' do
get '/' => redirect(subdomain: :new_subdomain, path: '')
end
So now subdomain_from.google.com will redirect to new_subdomain.google.com.

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}

How to override Rails app routes from an engine?

I have a Rails app that I am trying to integrate a Rails engine in to.
The host app has some catch all routes:
# magic urls
match '/' => 'admin/rendering#show'
match '*path/edit' => 'admin/rendering#show', :defaults => { :editing => true }
match '*path' => 'admin/rendering#show'
It looks like the engine routes are loaded after the application catches all routes.
/sitemap.xml(.:format) {:format=>"xml", :controller=>"admin/sitemaps", :action=>"show"}
/(.:format) {:controller=>"admin/rendering", :action=>"show"}
/*path/edit(.:format) {:controller=>"admin/rendering", :action=>"show"}
/*path {:controller=>"admin/rendering", :action=>"show"}
engine_envs GET /engine/envs/:id(.:format) {:controller=>"engine/envs", :action=>"show"}
PUT /engine/envs/:id(.:format) {:controller=>"engine/envs", :action=>"update"}
jammit /assets/:package.:extension(.:format) {:extension=>/.+/, :controller=>"jammit", :action=>"package"}
So far, everything is hitting the /engine/envs routes are getting caught by the application catch all routes. However I see that the jammit route is loaded after the engine and I don't believe those are getting caught. Any way to override the app routes?
You could stick your engine routes in a method and then call that in your host app.
# engine routes.rb
module ActionDispatch::Routing
class Mapper
def engine_routes
engine_envs GET /engine/envs/:id(.:format)
# ...
end
# ...
and then in your host app add the method before the catch-all route
# host app routes.rb
MyTestApp::Application.routes.draw do
# ...
engine_routes
match '/' => 'admin/rendering#show'
match '*path/edit' => 'admin/rendering#show', :defaults => { :editing => true }
match '*path' => 'admin/rendering#show'
end
Routes are used in the order they are defined. The first routes to be read are the one of the host application, then of your engine.
As soon as a matching route is found, the search for a route is stopped.
As far as I know, there are no way (I may be wrong about this) to override this feature other than to change your "mag
UPDATE: So that means that the order you see them in "rake routes" is the order they are processed. As soon as a matching route is found, there you go.

How do I redirect without www using Rails 3 / Rack?

I understand there are a lot of questions that answer this. I'm familiar with .htaccess and nginx.conf methods, but I do not have access to such traditional configuration methods on Heroku.
Simone Carletti gave this answer that leverages Rails 2.x Metals, but I'm using Rails 3 and this isn't compatible:
Redirect non-www requests to www URLs in Ruby on Rails
Please note:
I'm not looking for a simple before_filter in my ApplicationController. I'd like to accomplish a rewrite similar to Simone's. I believe this is job for the webserver or middleware like Rack at the very least, so I'd like to leave this bit out of the actual application code.
Goal
redirect to status
----------------------------------------------------
www.foo.com foo.com 301
www.foo.com/whatever foo.com/whatever 301
Only hosts matching /^www\./ should be redirected. All other requests should be ignored.
In Ruby on Rails 4, removing www. from any URL whilst maintaining the pathname can be achieved simply by using:
# config/routes.rb
constraints subdomain: 'www' do
get ':any', to: redirect(subdomain: nil, path: '/%{any}'), any: /.*/
end
In contrast, adding www. to the beginning of any URL that doesn't already have it can be achieved by:
# config/routes.rb
constraints subdomain: false do
get ':any', to: redirect(subdomain: 'www', path: '/%{any}'), any: /.*/
end
There's a better approach if you're using Rails 3. Just take advantage of the routing awesomeness.
Foo::Application.routes.draw do
constraints(:host => /^example.com/) do
root :to => redirect("http://www.example.com")
match '/*path', :to => redirect {|params| "http://www.example.com/#{params[:path]}"}
end
end
I really like using the Rails Router for such things. Previous answers were good, but I wanted something general purpose I can use for any url that starts with "www".
I think this is a good solution:
constraints(:host => /^www\./) do
match "(*x)" => redirect { |params, request|
URI.parse(request.url).tap {|url| url.host.sub!('www.', '') }.to_s
}
end
Take a look at this middleware, it should do precisely what you want:
http://github.com/iSabanin/www_ditcher
Let me know if that worked for you.
A one-line version of Duke's solution. Just add to the top of routes.rb
match '(*any)' => redirect { |p, req| req.url.sub('www.', '') }, :constraints => { :host => /^www\./ }
In Rails 3
#config/routes.rb
Example::Application.routes.draw do
constraints(:host => "www.example.net") do
match "(*x)" => redirect { |params, request|
URI.parse(request.url).tap { |x| x.host = "example.net" }.to_s
}
end
# ....
# .. more routes ..
# ....
end
If you want to redirect from the top-level domain (TLD) to the www subdomain, use this code:
constraints :subdomain => '' do
match '(*any)' => redirect { |p, req| req.url.sub('//', '//www.') }
end
Note: This code the use of sub, not gsub, because sub replaces the first occurrence of the double-slashes where gsub would replace all double-slashes.
For Rails 4 the above solutions have to be appended with the Verb construction e.g. via: [:get, :post]. Duke's solution becomes:
constraints(:host => /^www\./) do
match "(*x)" => redirect { |params, request|
URI.parse(request.url).tap {|url| url.host.sub!('www.', '') }.to_s
}, via: [:get, :post]
end
Nothing wrong with the approaches above, but there are also a couple of gems that provide Rack middleware to do this.
I like the way that they keep this behaviour separate from the app itself, but it's not a particularly strong argument either way. I also use middleware to do this when working with Sinatra, so prefer to use a technique that I can use on apps built from Rails and/or Sinatra (I often run Nesta embedded in Rails).
Anyway, here they are:
https://github.com/cwninja/rack-force_domain
https://github.com/tylerhunt/rack-canonical-host
The first is simpler (and the one I've been using) while the second offers a couple more features (that I'm yet to need, but appreciate).

Resources