Rails 5 Routing Error - uninitialized constant DashboardController - ruby-on-rails

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

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

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.

Related

How to fix Uninitialized Constant in Controller

I am getting a:
uninitialized constant ProfilesController::EUserPofile
error when trying to:
class ProfilesController < ApplicationController
def index
##profiles = EUserProfile.all
end
def preview
#profiles = EUserPofile.all
end
end
it works fine for index but for preview it crashes.
here is my route file:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get 'welcome', to: 'pages#home'
get 'profiles', to: 'profiles#index'
get 'login', to: 'login#login'
resources :profiles do
get 'preview', on: :member
end
end
There's a typo: EUserPofile -> EUserProfile

Routing error for rails - uninitialized constant SubscribersController

I have a Subscriber model that takes in a "phone_number" and a "visit" integer. I have two controllers Subscribers and Visits(super and sub) I have never worked with nested controllers before and I'm having some issues with namespace I believe. Because I getting back the uninitialized constant error. Basically the subscriber controller signs up a subscriber and the visit controller counts the amount of times they've visited by user input of their phone_number. Why am I getting this error? I'll show my code for clarity.
CONTROLLERS
class Subscribers::VisitsController < ApplicationController
def new
#subscriber = Subscriber.new
end
def create
#subscriber = Subscriber.find_by_phone_number(params[:phone_number])
if #subscriber
#subscriber.visit += 1
#subscriber.save
redirect_to subscribers_visits_new_path(:subscriber)
else
render "new"
end
end
end
class SubscribersController < ApplicationController
def index
#subscriber = Subscriber.all
end
def new
#subscriber = Subscriber.new
end
def create
#subscriber = Subscriber.create(subscriber_params)
if #subscriber.save
flash[:success] = "Subscriber Has Been successfully Created"
redirect_to new_subscriber_path(:subscriber)
else
render "new"
end
end
ROUTES
Rails.application.routes.draw do
devise_for :users
resources :subscribers, except: :show
get '/subscribers/visits/new', to: 'subscribers/visits#new'
root "welcomes#index"
VIEWS
<h1>hey</hey>
<%= form_for #subscriber do |form| %>
<div class="form-group">
<p>
<%= form.label :phone_number %>
<%= form.text_field :phone_number %>
</p>
<% end %>
ERROR
Hmm, my guess is you are trying to route url subscriber/visits/new to new action in VisitsController?How about changing this line:
get '/subscribers/visits/new', to: 'subscribers/visits#new'
to:
namespace :subscribers do
get '/visits/new', to: 'visits#new'
end
Also try to move this block above resources :subscribers, except: :show if you still get the error.
Cheers
You probably do not need to inherit one controller from another. Simply define the controllers as you normally would:
app/controllers/subscribers_controller.rb
class SubscribersController < ApplicationController
# methods for Subscribers
end
in app/controllers/visits_controller.rb
class VisitsController < ApplicationController
# methods for Visits
end
Note that these must to be located in separate files, so that Rails can find the correct source file by the name of the object that it's looking for. This is a Rails naming convention.
Regarding your routes, you'll need to change to use one of 4 route formats. Reading the section on Adding More RESTful Actions in the Rails Routing from the Outside In guide might help.
1) To route visits as a nested resource, which is what it appears you're actually trying to do, you would use this:
resources :subscribers, except: :show do
resources :visits
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/:id/visits/new
POST /subscribers/:id/visits
GET /subscribers/:id/visits
GET /subscribers/:id/visits/:id
GET /subscribers/:id/visits/:id/edit
PATCH /subscribers/:id/visits/:id/update
DELETE /subscribers/:id/visits/:id/destroy
This is the typical route structure for nested resources and separate controllers.
2) To make visits#new a simple collection (non-member) action in the VisitsController, then you likely want this:
resources :subscribers, except: :show do
collection do
get 'visits/new', to 'visits#new'
post 'visits', to 'visits#create'
end
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/visits/new
POST /subscribers/visits
This is typically used to add new top-level routes in an existing resource and controller.
3) To construct visits as member actions, use this:
resources :subscribers, except: :show do
member do
get 'visits/new', to 'visits#new'
post 'visits', to 'visits#create'
end
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/:id/visits/new
POST /subscribers/:id/visits
This is normally used to add new member routes in an existing resource and controller.
4) To simply make visits routes appear to be included in subscribers, you could use this:
get '/subscribers/visits/new', to: 'visits#new'
post '/subscribers/visits', to: 'visits#create'
resources :subscribers, except: :show
This will produce these routes:
GET /subscribers/visits/new
POST /subscribers/visits
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
This may be used to make arbitrary routes appear to be included in an existing resource, when they really may be independent.

rails route error undefined local variable or method

I'm a beginner of RoR and this is my sample
class PagesController < ApplicationController
def welcome
end
end
in my route file
Rails.application.routes.draw do
root :to => “pages#welcome”
...
end
when I open powder
#config/routes.rb
root to: "pages#welcome"

Routing Error uninitialized constant when I create a new action

I know it is a typical problem. But I was just trying the routing in rails and I get this error:
uninitialized constant UsersController
And I don't know well where is the problem.
The resume of my app is a simple application of a library, where users can book books and see the books they've booked.
Here is my routes.rb
Brickstest2::Application.routes.draw do
resources :books
root "pages#home"
get "home", to: "pages#home", as: "home"
get "inside", to: "pages#inside", as: "inside"
devise_for :users
namespace :admin do
root "base#index"
resources :users
end
resources :books do
get :book_a_book, :as => "reserve"
end
resources :users do
get :booked_books, :as => "reserved", :on => :member
end
end
Actually When I do rake routes I get:
reserved_user_path
GET /users/:id/booked_books(.:format) users#booked_books
And in users_controllers.rb I have:
def booked_books
#user = User.first(params[:user_id])
#return unless #user == current_user
#books = Books.where(:user = #user)
end
The link to booked_books:
<%= link_to "My books", user_reserved_path(:user_id => current_user.id)%>
And finally my users/booked_books.html.erb:
<h1>My books</h1>
<%= #books.each do |book|%>
<p><%= book.name %>
<% end %>
Thanks in advance.
Have you declared a controller for your User resource that subclasses ApplicationController?
# app/controllers/users_controller.rb
class UsersController < ApplicationController
Note that the precise naming convention for a plural resource route for the User model is UsersController (note the s). I'm guessing that you've named your controller class UserController, which won't work for your use-case.
UPDATE:
Since you've declared a controller namespace, you'll want to declare your controller class like so:
# app/controllers/admin/users_controller.rb
class Admin::UsersController < ApplicationController
This should resolve any uninitialized constant UsersController errors involving your namespace.

rails controller with namespace with shorter url

Here is my code
namespace :appname do
resources :docs do
collection do
get 'contact'
get 'how_it_works'
get 'terms'
get 'privacy'
end
end
end
It generates
/appname/docs/contact
/appname/docs/how_it_works
/appname/docs/privacy
/appname/docs/terms
But how to make them as
/docs/contact
/docs/how_it_works
/docs/privacy
/docs/terms
my controller code
class Appname::DocsController < ApplicationController
def how_it_works
end
def privacy
end
def contact
end
def terms
end
end
defined the routes as follow
scope module: 'appname' do
resources :docs do
collection do
get 'contact'
get 'how_it_works'
get 'terms'
get 'privacy'
end
end
end
you can get more info from the rails routing guide in the namespace section. http://guides.ruby-china.org/routing.html
Remove the outer namespace :appname ... end block.

Resources