设计师在注册时创建一个子域名 [英] Have Devise create a subdomain on registration

查看:137
本文介绍了设计师在注册时创建一个子域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让Devise在我的网站上创建子域名。

I would like to have Devise create subdomains on my site.

现在,我有两个模型:


  1. 公司:A 公司可以直接在网站上注册,登录后可以邀请他们的员工。当公司注册时,我想要创建一个唯一的子域(例如example.com => techcraz.example.com。)

  1. Company: A Company can register directly on the site, and after signing in, can invite their employees. When the company registers, I want a unique subdomain to be created (e.g. example.com => techcraz.example.com.)

员工: c $ c> Employee 只有在收到邀请链接时才可以注册。

Employee: An Employee can register only if they received an invitation link.

我想:


  • 作为注册页面的主域名。

  • 单个登录页面对于公司员工

  • 登录时必须提供域名,那么他们应该被重定向到该子域的登录页面(例如techcraz.example.com/signin。)

  • 输入不存在的URL时,应将其重定向对于注册页面。

  • The main domain as a registration page.
  • A single sign in page for both Companies and Employees.
  • When signing in they must provide a domain name then they should be redirected to the sign in page for that subdomain (e.g. techcraz.example.com/signin.)
  • When entering a URL that does not exist, they should be redirected to the registration page.

我是Rails的新手。请帮助!

I am new to Rails. Please help!

提前感谢

推荐答案

用户是Web应用程序开发中相当常见的用例。以下是您可以如何做:

One subdomain per user is a fairly common use-case in web application development. Here's how you can do it:

首先:确保您的用户表有一个:名称列(我认为Devise默认情况下执行此操作 - 如果不能,您可以运行 rails g migration AddNameToUsers name:string 将此列添加到数据库)

First: ensure your Users table has a :name column (I think Devise does this by default - if not you can run rails g migration AddNameToUsers name:string to add this column to your database).

要使用这个 User.name 作为子域,我们需要确保它只包含字母数字字符(带可选下划线)。我们也会将该名称限制为最多32个字符。最后,我们不希望用户选择诸如www之类的名称,这些名称会导致诸如 http:// www .myapp.com 。以下是 app / models / user.rb 的验证:

To use this User.name as a subdomain we’ll need to make sure it only contains alphanumeric characters (with an optional underscore). We’ll also limit the name to a maximum of 32 characters. Finally, we don’t want users to choose names such as "www" that will result in URLs such as "http://www.myapp.com". Here's the validations for app/models/user.rb:

validates_format_of :name, with: /^[a-z0-9_]+$/, 
  message: "must be lowercase alphanumerics only"

validates_length_of :name, maximum: 32, 
  message: "exceeds maximum of 32 characters"

validates_exclusion_of :name, in: ['www', 'mail', 'ftp'], 
  message: "is not available"

(可选)修改db / seeds.rb(这样就可以在初始化数据库时创建测试用户)

Optionally: modify your db/seeds.rb (so it creates test users when you initialize the database):

user = User.create! :name => 'myname', :email => 'user@example.com', 
  :password => 'password', :password_confirmation => 'password'

当任何人进入具有子域的网址时,我们将显示用户的个人资料页面匹配现有用户 app / controllers / profiles_controller.rb

We’ll display a profile page for a user when anyone enters a URL with a subdomain that matches an existing user app/controllers/profiles_controller.rb:

class ProfilesController < ApplicationController
  def show
    @user = User.where(:name => request.subdomain).first || not_found
  end

  def not_found
    raise ActionController::RoutingError.new('User Not Found')
  end
end

以下是视图的示例文件 app / views / profiles / show.html.erb

Here's an example file for the view app/views/profiles/show.html.erb:

<h1>Profile</h1>
<h3><%= @user.name %></h3>
<h3><%= @user.email %></h3>

最后,我们需要为子域执行路由。创建一个这样的类:

Lastly we need to implement routing for the subdomains. Create a class like this:

class Subdomain
  def self.matches?(request)
    case request.subdomain
    when 'www', '', nil
      false
    else
      true
    end
  end
end

确保此类在应用程序启动时自动加载 config / application.rb

Make sure this class is autoloaded when the application starts config/application.rb:

config.autoload_paths += %W(#{config.root}/lib)

确保您的 routes.rb 文件包含以下路由:

Ensure your routes.rb file contains the following routes:

devise_for :users
resources :users, :only => :show
constraints(Subdomain) do
  match '/' => 'profiles#show'
end

如果您使用 rails生成为您的配置文件控制器 - 确保您删除获取配置文件/显示路线。

If you used rails generate for your profiles controller - ensure that you remove the get "profiles/show" route.

请参阅此页面,了解有关在应用程序中使用URL Helpers的信息(基本上您需要使用 new_user_session_url 而不是 new_user_session_path ,您可以指定这样的子域:

See this page for information on using URL Helpers in your application (essentially you'll need to use new_user_session_url instead of new_user_session_path and you can specify a subdomain like this:

root_url(:subdomain => @subdomain)

这篇关于设计师在注册时创建一个子域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆