我正在尝试为用户提供创建子域的能力。单击创建帐户时,系统会从“accounts/new”重定向到“accounts”,并且不会填充子域。
路线.rb
class SubdomainPresent
def self.matches?(request)
request.subdomain.present?
end
end
class SubdomainBlank
def self.matches?(request)
request.subdomain.blank?
end
end
Saasapp::Application.routes.draw do
constraints(SubdomainPresent) do
root 'projects#index', as: :subdomain_root
devise_for :users, controllers: { registrations: 'users/registrations' }
resources :users, only: :index
resources :projects, except: [:index, :show, :destroy]
end
constraints(SubdomainBlank) do
root 'visitors#new'
resources :accounts, only: [:new, :create]
end
end
account_controller.rb
class AccountsController < ApplicationController
skip_before_action :authenticate_user!, only: [:new, :create]
def new
@account = Account.new
end
def create
@account = Account.new(account_params)
if @account.valid?
Apartment::Database.create(@account.subdomain)
Apartment::Database.switch(@account.subdomain)
@account.save
redirect_to new_user_session_url(subdomain: @account.subdomain)
else
render action: 'new'
end
end
新的.html.erb
<%= simple_form_for @account do |f| %>
<%= f.input :subdomain do %>
<div class="input-group">
<%= f.input_field :subdomain, class: 'form-control' %>
<span class="input-group-addon">.demo.dev</span>
</div>
<% end %>
<%= f.button :submit, class: 'btn-primary' %>
<% end %>
日志中的错误
Started GET "/accounts.json" for 127.0.0.1 at 2017-02-08 21:56:08 -0500
ActionController::RoutingError (No route matches [GET] "/accounts.json"):