简体   繁体   中英

how do i use subdomain in rails to redirect users to thier domains

I want to use a subdomain to redirect users to their subdomain

example

https://usersubdomain.mydomain.com

My setup is as fellow,

Each user has an account which is created when a new user is registered

class User < ApplicationRecord
.........
     has_one :account
      after_initialize :set_account

         def set_account
           build_account unless account.present?
         end
 ......        
end

The account has subdomain attributes which I intend to use as the Url for the user

create_table "accounts", force: :cascade do |t|
    t.string "subdomain"
    t.bigint "user_id"
    ..........

On user creation, I used nested attributes to create the subdomain for the account. I am using devise

User Registration.

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

  <%= f.fields_for :account do |a| %>
    <div class="field">
    <%= a.label :subdomain %><br />
    <%= a.text_field :subdomain%>
     ..........

Resources are scoped through account

class ApplicationController < ActionController::Base
    before_action :set_account

    def require_account!
        redirect_to home_index_url(subdomain: nil) if @account.nil?
    end
    def set_account
      @account = Account.find_by(subdomain: request.subdomain)
    end

routes.rb

Rails.application.routes.draw do
  devise_for :users
  resources :posts
  root to: 'posts#index'

  get 'home/index'
end

With this setup. I need to manually enter the subdomain to get the page of the user. If not I am always redirected to the home index page as intended.

I use lvh.me on the localhost. So must enter subdomain.lvh.me:3000 on the address bar to get the user page. So how can the subdomain be added automatically when users sign in Thanks

All The controllers are scoped with

@resource = @accout.resoures

I am trying to build a multitenant app

I get the impression that since you know how to get the subdomain from the request and from the database what you are having problem is building the URL to where you want to send the user from the on right? if that is the case you might be looking for url_for handling the this in detail https://api.rubyonrails.org/v5.2.2/classes/ActionDispatch/Routing/UrlFor.html

for example

Rails.application.routes.url_helpers.url_for(controller: "admin/users", action: 'show', id: 1, host: 'somehost.org', port: '8080', subdomain: "yadah")
=> "http://yadah.somehost.org:8080/admin/users/1"

but also you can do users_url(subdomain: user.subdomain) .

Dont forget you can use regular expression in you constraints too

 constraints: {subdomain: /.+/}

or a lambda

constraints: lambda { |r| r.subdomain.present? && r.subdomain != 'www' }

user this with devise's after sign in path

class ApplicationController < ActionController::Base
  protected
    def after_sign_in_path_for(resource)
      request.env['omniauth.origin'] || stored_location_for(resource) || root_path
    end
end

https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM