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
Related
I've made authorization following this tutorial: https://www.railstutorial.org/book/sign_in_out#cha-sign_in_sign_out but now I want to add subdomains to my application.
I've added this to my routes.rb:
match '/' => 'students/board', :constraints => { :subdomain => 'student' }, via: 'get'
If I want to redirect user after sign in to his subdomain like this:
redirect_to :subdomain => 'student', :path => '/'
I'm getting this error:
No route matches [GET] "/sessions"
If I redirect user without a subdomain he is normally redirected. I don't understand why it's trying to get 'sessions' path. I would be grateful for some suggestions. I didn't find anything online which is related to login sessions and subdomains.
Thanks!
There might be something in your code you aren't showing us redirecting you to the sessions path.
Have you tried naming the path then redirecting there?
routes.rb:
get '/' => 'students/board', :constraints => { :subdomain => 'student' }, as: 'student_login'
then use:
redirect_to student_login_path
I have a Rails 3.1 multi-tenancy app with a domain that I'll call mydomain.com. With this I would like to create the following routes but keep coming unstuck
default root for www.mydomain.com and mydomain.com should go to a controller called home or similar
default root for *.mydomain.com (except www) should go to a sesions/new route
default root for *.mydomain.com (except www) when logged in will go to a dashboard controller or simialar
Can anyone help with a way of achieving this?
This is pretty similar to what you're looking for: http://maxresponsemedia.com/rails/setting-up-user-subdomains-in-rails-3/.
Edit
It appears that the link is now dead (which is why we should post more than just links!), but I was able to find it in the WayBackMachine. Here are the code examples that it had.
First, we define a couple of constraints for subdomains and the root domain:
# /lib/domains.rb
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != "www" && request.subdomain != ""
end
end
class RootDomain
#subdomains = ["www"]
def self.matches?(request)
#subdomains.include?(request.subdomain) || request.subdomain.blank?
end
end
Then, in our routes.rb, we direct the subdomains to a websites controller, but any requests to domains related to the main site get sent to the static pages that are configured for the app.
# config/routes.rb
# a bunch of other routes...
# requiring the /lib/domains.rb file we created
require 'domains'
constraints(Subdomain) do
match '/' => 'websites#show'
end
constraints(RootDomain) do
match '/contact_us', :to => 'static_pages#contact'
match '/about', :to => 'static_pages#about'
match '/help', :to => 'static_pages#help'
match '/news', :to => 'static_pages#news'
match '/admin', :to => 'admin#index'
end
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
Update: rewritten question a bit. Trying to route my subdomains like below
login.app.ltd
user1.app.ltd
user2.app.ltd
signup.app.ltd
Using
Rails 3.2
Devise
To no avail tried several tutorials blog posts, anyone knows a working example for this?
Really stuck on this :(
this is my routes now:
match '', to: 'frontend#index', constraints: lambda { |r| r.subdomain.present? && ( r.subdomain != 'www') }
#match '' => 'home#index', :constraints => { :subdomain => 'login' }
constraints :subdomain => /^(?!signup\b)(\w+)/ do
root :to => "frontend#index"
end
root :to => "frontend#index"
My RailsApps project offers a complete example app showing how to use subdomains:
Rails Tutorial for Subdomains with Devise
Did you take a look at that?
config/routes.rb
devise_for :users
resources :users, :only => :show
constraints(Subdomain) do
match '/' => 'profiles#show'
end
root :to => "home#index"
lib/subdomain.rb
class Subdomain
def self.matches?(request)
case request.subdomain
when 'www', '', nil
false
else
true
end
end
end
Ok with some help managed to get it working
One should do:
constraints subdomain: 'login' do
devise_scope :user do
root to: 'sessions#new'
end
end
I have successfully added the ability to use dynamic subdomains within my application. The issue is that when I run my Cucumber tests, I receive the following error when my application performs a redirect_to which contains a subdomain:
features/step_definitions/web_steps.rb:27
the scheme http does not accept registry part: test_url.example.com (or bad hostname?)
I have a signup controller action that creates the user and the account chosen and redirects the the user to the logout method with the subdomain specified based on what the user selected as the subdomain in the sign up form. Here is the code for the redirect action that happens once the user and account models are created and saved:
redirect_to :controller => "sessions", :action => "destroy", :subdomain => #account.site_address
Here are my rails 3 routes:
constraints(Subdomain) do
resources :sessions
match 'login', :to => 'sessions#new', :as => :login
match 'logout', :to => 'sessions#destroy', :as => :logout
match '/' => 'accounts#show'
end
Here is the code I have so far for the Subdomain class which is specified in the constraint above:
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != "www"
end
end
I added UrlHelper to the ApplicationController:
class ApplicationController < ActionController::Base
include UrlHelper
protect_from_forgery
end
This is the code for the above UrlHelper class:
module UrlHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
[subdomain, request.domain, request.port_string].join
end
def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
end
super
end
end
All of this code above allows me to run subdomains fine within the local browser. The issue above happens when I run my Cucumber test. The test clicks the signup button which in turn calls the redirect_to and throws the exception listed above.
Here is what my gem file looks like:
require 'subdomain'
SomeApp::Application.routes.draw do
resources :accounts, :only => [:new, :create]
match 'signup', :to => 'accounts#new'
constraints(Subdomain) do
resources :sessions
match 'login', :to => 'sessions#new', :as => :login
match 'logout', :to => 'sessions#destroy', :as => :logout
match '/' => 'accounts#show'
end
end
Could you please let be know an additional method to have my tests work now? I would be interested in either a fix or a way I can test my methods without using subdomains (for example a mocked out method that retrieves the account name).
I have this same pattern in my code. I use Capybara (but not Cucumber), and I was able to get around it like this:
# user creates an account that will have a new subdomain
click_button "Get Started"
host! "testyco.myapp.com"
# user is now visiting app on new subdomain
visit "/register/get_started/" + Resetkey.first.resetkey
assert_contain("Get Started Guide")
The host! command effectively changes the host as it appears to the app from the test request.
EDIT: Just realized this was working with webrat, but not capybara (i'm using both, phasing out webrat now.) The way I'm doing it in capybara is to either click a link to a new domain (capybara follows it) or to:
visit "http://testyco.myapp.com/register"
EDIT: Another update. Found a method that works without having to use the full URL in every event.
host! "test.hiringthing.com"
Capybara.app_host = "http://test.hiringthing.com"
In the test setup.