Rails 和公寓 - 创建租户后重定向

Rails and Apartment - Redirect after creating the tenant

我有一个用于创建租户的域模型。

class Domain < ApplicationRecord
 after_create :create_tenant
 def create_tenant 
  Apartment::Tenant.create(name) 
 end
end

创建租户后 "example" 我想将我的浏览器自动重定向到 http://example.lvh.me:3000

有人可以帮我解决这个问题吗?

答案比我想象的要简单。 成功创建租户后,只需使用您的子域名重定向到 url。 修改域控制器中的创建定义

def create
@domain = Domain.new(domain_params)


respond_to do |format|
  if @domain.save
    format.html { redirect_to "http://#{@domain.name}.lvh.me:3000/users/sign_in", notice: 'Domain was successfully created.' }
  else
    format.html { render :new }
    format.json { render json: @domain.errors, status: :unprocessable_entity }
  end
end
end

添加了这一行

redirect_to "http://#{@domain.name}.lvh.me:3000/users/sign_in"

重定向到特定的 url

redirect_to user_sign_in_url(subdomain: domain.name)

使用 URL 而不是路径是更优雅的方法,rails 方法。