I have this in my routes.rb file:
class SubdomainWww
def self.matches?(request)
request.subdomain.start_with? "www."
end
def self.strip_www(subdomain)
if subdomain.start_with? "www."
subdomain.slice!(4..-1)
else
subdomain
end
end
end
MyApp::Application.routes.draw do
constraints(SubdomainWww) do
match '*path', :to => redirect(:subdomain => SubdomainWww.strip_www(???))
match '/', :to => redirect(:subdomain => SubdomainWww.strip_www(???))
end
...
The purpose if this is to remove the www. for subdomains (e.g. www.sub.domain.tld should be redirected to sub.domain.tld; the subdomain is later used to identify the client). How can I replace the '???' so that the subdomain(string) of the current request is passed to the function strip_www()?
Thanks in advance!
Don't know if this will work or help, but I had the same problem using Cells with Devise, where the Devise current_* methods would want env and warden which are stored in the request. I had a route to a Cell (Controller and View) which rendered the Cell's View for me. The way I got the request into the Cell's Controller was to pass self in as request from route.rb.
match "/dashboard/widget/:name" => proc { |env|
cell_name = env["action_dispatch.request.path_parameters"][:name]
[ 200, {}, [ Cell::Base.render_cell_for(cell_name, :display, :request => self) ]]
}
Note the :request => self. When you're in routes, self is the ActionDispatch::Request object itself. Just pass request along and you should be good.
MyApp::Application.routes.draw do
constraints(SubdomainWww) do
match '*path', :to => redirect(:subdomain => SubdomainWww.strip_www(self))
match '/', :to => redirect(:subdomain => SubdomainWww.strip_www(self))
end
...