How to remove subdomain from root path in Rails - ruby-on-rails

I want to remove the subdomain from root path.
I tried adding :subdomain => false to the root command in routes.rb file without success: when I enter manually a subdomain in the URL, the subdomain stays and will not be removed.
Example:
my root is => lvh.me:3000
enter subdomain manually => xyz.lvh.me:3000 and hit enter then it remains the same
This is what I tried already in my routes.rb file, without success:
root :to => 'home#show', :subdomain => false or
root :to => 'home#show', :constraints => { :subdomain => false }, via: [:get]

write this in application controller. this will work.
before_action :check_subdomain
def check_subdomain
unless company_signed_in?
if request.subdomain.present? && params[:controller] == "companies/registrations" && params[:action] == "new"
redirect_to root_url, subdomain: false
end
end
end

According to this comment on rails github, in Rails >= 4 you need to use constraints to obtain this. Try this:
constraints subdomain: false do
root to: 'home#show'
end

#dgilperez: your code works great, but I need to change in application controller too, Yes, I found the solution, I just updated with
before_action :check_subdomain
def check_subdomain
unless company_signed_in?
if request.subdomain.present? && params[:controller] == "companies/registrations" && params[:action] == "new"
redirect_to root_url, subdomain: false
end
end
end

Related

Overwrite root route in rails 4 for a certain subdomain

I'd like so overwrite my root :to => redirect("/projects") route if the subdomain is equal to "www" or is just "". This means if the subdomain is "www" or "" the app should use my website#index controller/action.
At the moment I got this setup:
class Subdomain
def self.match(r)
r.subdomain == "www" || r.subdomain == ""
end
end
.....
# website
scope :constraints => lambda { |request| Subdomain.match(request) } do
get '/' => 'website#index'
get "/help" => "website#help", as: "help"
get "/about" => "website#about", as: "about"
get "/signup" => "website#signup", as: "signup"
# post "/signup" => "website#signup_account"
end
root :to => redirect("/projects")
If I access www.satisfy.dev/help or satisfy.dev/help everything works fine. If I access www.satisfy.dev/ or satisfy.dev/ the root_path (/projects) is in use. I thought the get '/' => 'website#index' should be more important than the root_path since it's above the root_path.
Hope somebody has a hint for me!
I tried treating the routes as "get" requests and had more success.
See here:
Multiple 'root to' routes in rails 4.0

How to do route constraints in Rails 4

This is not working:
get '/' => 'addresses#show', :constraints => Subdomain
lib/subdomain.rb:
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != 'www'
end
end
this route is just ignored...
thank you
Solution:
the specified route has to go before
root to: 'home#index'
This worked perfectly only you need to add subdomain.rb in you routes manually
get '/' => 'addresses#show', :constraints => Subdomain
lib/subdomain.rb:
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != 'www'
end
end
Add in routes.rb
require 'subdomain'
And yes the specified route has to go before
root to: 'home#index'

Force a route to exit a subdomain in Rails 3.2

I'm struggling with exiting out of a subdomain back up to the root domain in Rails 3.2.
Say I have a blog with a dashboard. Each user has a subdomain at username.blog.com. Each user also has a dashboard at blog.com/dashboard.
If a user manually types in username.blog.com/dashboard, I want them to be redirected to blog.com/dashboard.
I've tried using subdomain => false in my routes, but it seems to be of no use. I also tried a matcher underneath the domain scope, but that also didn't work. Any help would be greatly appreciated!
Relevant routes.rb follows.
resource :dashboard, :controller => 'dashboard', :subdomain => false
scope '/', constraints: lambda { |r| r.subdomain.present? && r.subdomain != 'www' } do
get '/' => 'feed#show'
end
root :to => 'dashboard#show', :subdomain => false
I would keep that logic outside the routing.
I would use dashboard_url instead of dashboard_path in my views / controllers.
I would put something like this in my ApplicationController
def dashboard_url(options={})
options[:subdomain] = false
super(options)
end

Include subdomain for constrained route in Rails url helper

Say I have the following routes that are constrained to specific subdomains:
App::Application.routes.draw do
constraints :subdomain => "admin" do
scope :module => "backend", :as => "backend" do
resources :signups
root :to => "signups#index"
end
end
constraints :subdomain => "www" do
resources :main
root :to => "main#landing"
end
end
My problem is that root_url and backend_root_url both returns a url on the current subdomain: "http://current-subdomain.lvh.me/" instead the subdomain specific for the resource.
I would like root_url to return "http://www.lvh.me/" and backend_root_url to return "http://admin.lvh.me/" (the behavior should be the same for all resources under the subdomain).
I have tried to accomplish this in rails 3.2 by setting the url options in various places, one being url_options in application controller:
class ApplicationController < ActionController::Base
def url_options
{host: "lvh.me", only_path: false}.merge(super)
end
end
Maybe I need to override the url helpers manually? How would I approach that (accessing the routes etc)?
Edit: I'm able to get the correct result using root_url(:subdomain => "admin") which returns "http://admin.lvh.me/" regardless of the current subdomain. However, I would prefer not having to specify this all over the code.
Using "defaults" as shown below will make rails url helpers output the correct subdomain.
App::Application.routes.draw do
constraints :subdomain => "admin" do
scope :module => "backend", :as => "backend" do
defaults :subdomain => "admin" do
resources :signups
root :to => "signups#index", :subdomain => "admin"
end
end
end
constraints :subdomain => "www" do
defaults :subdomain => "www" do
resources :main
root :to => "main#landing"
end
end
end

How do I redirect to root - public/index.html?

I wish to do a redirection to index.html in my application/public folder.
def get_current_user
#current_user = current_user
if #current_user.nil?
redirect_to root_path
end
end
How do I achieve this ?
I haven't modified the root in my routes.rb ( Its still commented )
# root :to => "welcome#index"
I get an error saying root_path is undefined.
How do I modify routes.rb so that root_path points to public/index.html ?
You can assign a named route to a static file by passing any non-empty string as :controller and the path to the file as the :action for the route:
Application.routes.draw do
root :controller => 'static', :action => '/'
# or
# root :controller => 'static', :action => '/public/index.html'
end
# elsewhere
redirect_to root_path # redirect to /
Assuming you have a public/index.html, this is what will be served.
on controller
redirect_to root_path ## (will redirect to root '/')
route file:
root 'main#index'
controller:
class MainController < ApplicationController
def index
redirect_to '/index.html'
end
end
and using rails 4 controller action live this can behave like a single page application using the M & C with a twist on the V
What you want to do is not Rails compatible.
Rails is MVC, C for controller, V for view.
So its internals need both.
Ok, public/index.html is displayed by default but it's just because process is bypassed.
So, you could create a static controller with an index action and it's corresponding view (just copy/paste the content of your current public/index.htmlfile in it).
Then set:
root :to => "static#index"
And please, remove the public/index.html file :)
routes.rb
...
root to: redirect('public/index.html')
...
This will redirect all request to '/', to 'public/index.html'.

Resources