我正在构建一个多租户网站,并使用Ryan Bigg的https://leanpub.com/multi-tenancy-rails
作为指南。我坚持创建一个简单的范围路线;我一直收到错误Rails 5 Routing Error - uninitialized constant DashboardController
。我确信我遗漏了一些简单的东西,因为语法自出版物的最终版本发生了一些变化。我的代码下面是否应该如此 - 现在我只是想要显示仪表板索引页面?
控制器/帐户/ base_controller.rb
module Accounts
class BaseController < ApplicationController
before_action :authenticate_user!
end
end
控制器/帐户/ dashboard_controller.rb
module Accounts
class DashboardController < Accounts::BaseController
def index
end
end
end
视图/帐户/仪表板/ index.html.rb
<section class="bg--secondary space--sm conversation">
<div class="container">
<div class="row">
<div class="col-md-6">
<h1>Dashboard</h1>
</div>
</div>
</div>
</section>
的routes.rb
constraints(SubdomainRequired) do
scope module: 'accounts' do
root to: 'dashboard#index', as: :account_root
end
end
Rails.application.routes.draw do
devise_for :users
constraints(SubdomainRequired) do
root to: 'dashboard#index', as: :account_root
end
get '/accounts/new', to: 'accounts#new', as: :new_account
post '/accounts', to: 'accounts#create', as: :accounts
root 'welcome#index'
end
答案 0 :(得分:2)
我很快就制作了多租户项目,并将lvh.me用于开发环境。尝试以下解决方案:
<强>配置/初始化/ subdomain_constraint.rb 强>
class SubdomainConstraint
def initialize
@domain_development = 'lvh.me'
end
def matches?(request)
if Rails.env.development?
@domain_development == request.domain
end
end
end
<强>的routes.rb 强>
constraints(SubdomainConstraint.new) do
match '', to: 'dashboard#index', constraints: {subdomain: /.+/}, via: [:get]
end
答案 1 :(得分:1)
我不确定您是否可以在routes.rb
文件中定义一个类。也许如果您创建一个名为lib/subdomain_required.rb
并移动
class SubdomainRequired
def self.matches?(request)
request.subdomain.present? && request.subdomain != 'www'
end
end
到该文件,而不是将其放入routes.rb
。