Charles Smith
Charles Smith

Reputation: 3289

Rails 5 Routing Error - uninitialized constant DashboardController

I'm building out a multi-tenant site and am using Ryan Bigg's https://leanpub.com/multi-tenancy-rails as a guide. I am stuck at creating a simple scoped route; I keep getting the error Rails 5 Routing Error - uninitialized constant DashboardController. I am sure I am missing something simple as syntax has changed a bit since the final release of the publication. Does my code below look as it should--right now I am just looking to get the dashboard index page to show?

controllers/accounts/base_controller.rb

 module Accounts
   class BaseController < ApplicationController
     before_action :authenticate_user!
   end
 end

controllers/accounts/dashboard_controller.rb

module Accounts
  class DashboardController < Accounts::BaseController
    def index
    end
  end
end

views/accounts/dashboard/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

Upvotes: 0

Views: 341

Answers (2)

Hamdi Bayhan
Hamdi Bayhan

Reputation: 1833

I made multi tenant project soon and I had used lvh.me for development environment. Try the following solution:

config/initializers/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

Upvotes: 2

Josh
Josh

Reputation: 676

I'm not sure you can define a class in the routes.rb file. Perhaps if you create a new file called lib/subdomain_required.rb and moved

class SubdomainRequired
  def self.matches?(request)
    request.subdomain.present? && request.subdomain != 'www'
  end
end

to that file instead of putting it in your routes.rb.

Upvotes: 1

Related Questions