I have a rails application using devise for authentication. It works when deployed locally but when push to Heroku I get this error
2014-07-03T14:20:17.235816+00:00 app[web.1]: /app/app/controllers/users_controller.rb:1:in `<top (required)>': uninitialized constant Users (NameError)
Here is the file it is referring to
class Users::SessionsController < Devise::SessionsController
before_create :create_associated_records
ROLES = [ROLE_STUDENT = 'student', ROLE_GRADUATE = 'graduate',
ROLE_INSTITUTIONAL_ADMIN = 'role_recruiter', ROLE_ADMIN = 'admin']
def create
#user = User.create(user_params)
end
private
def user_params
params.require(:user).permit(:avatar)
end
end
Thank you.
I believe you should either move
/app/app/controllers/users_controller.rb
to
/app/app/controllers/users/users_controller.rb
or change
class Users::SessionsController < Devise::SessionsController
to
class SessionsController < Devise::SessionsController
This is due to the ::, which denotes a location. Users::SessionsController would be looking for ./Users/SessionController, which is a relative path to the current tree level (controllers).
Related
I changed my controller path like this.
and I have specified folder path exactly like this
class Admin::UserFoodController < Admin::ApplicationController
class Admin::UsersController < Admin::ApplicationController
class Admin::ApplicationController < ActionController::Base
but, when I restart unicorn, I got error
/home/ubuntu/hid/admin/shared/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:283:in `const_get': uninitialized constant ApplicationController (NameError)
from /home/ubuntu/hid/admin/shared/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:283:in `block in constantize'
from /home/ubuntu/hid/admin/shared/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:281:in `each'
so I made dummy ApplicationController in controller folder, not in admin folder
error has gone.
I don't want to make dummy ApplicationController in controller folder. I wanna keep this folder structure.
what should I do?
help me plz...
I found.
because of devise gem.
I had to change some code in devise.rb(~/2.6.4/gems/lib/devise/devise.rb)
before = > "ApplicationController"
after => "Admin::ApplicationController"
thanks all
inside app/controllers/admin/user_food_controller.rb, here is sample you can use
require_dependency "admin/application_controller"
module UserFoodController
class UserFoodController < ApplicationController
...
end
end
by using require_dependency ensures that classes are loaded and unloaded in the correct manner.
the settings above "seems similar" with setting using rails engine and here link in case you need to learn more
I have little problem about my code I make controller/admin/moderators_controller.rb.
I got an error on compilator uninitialized constant Admin (NameError) in moderators_controller.rb.
In navigator I got this error:
superclass must be a Class (Module given)
navigator error
class Admin::ModeratorsController < ActionController
def index
end
end
This is my routes.rb file:
Rails.application.routes.draw do
namespace :admin do
resources :moderators, only: [:index]
end
end
You’ve accidentally made your controller inherit from ActionController (a module) instead of ActionController::Base (a class). You need to add ::Base to the end there.
If this is Rails 5, the common convention now is to have a ApplicationController class in your app/controllers folder, and have all controllers inherit from that (it’s just a class that inherits from ActionController::Base, but gives you a place to put common methods).
Rails 5
class Admin::ModeratorsController < ApplicationController
def index
end
end
Rails 4 or below
class Admin::ModeratorsController < ActionController::Base
def index
end
end
I'm trying to create a login system with devise, cancancan and rolify. The devise part is working. I recently added cancancan and rolify and I'm trying to test if they are working.
My cancancan ability file:
class Ability
include CanCan::Ability
def initialize(user)
if user.has_role? :admin
can [:index], Page
else
can [:index,:lecture]
end
end
end
I have a root page root to: "pages#index" which is working. If I add load_and_authorize_resource at the top of the pages controller like
class PagesController < ApplicationController
load_and_authorize_resource
def index
end
def lecture
end
end
I get back when trying to access it:
NameError in PagesController#index generate by these lines:
names.inject(Object) do |constant, name|
if constant == Object
* constant.const_get(name)
else
candidate = constant.const_get(name)
next candidate if constant.const_defined?(name, false)
The * is the line that gives the error. I also saw in the rolify documentation that I have to add resourcify to the files that are going to make use of it. But if I try to add it I get an error undefined method resourcify. How can I solve this ?
Check the following:
GemFile:
gem 'load_and_authorize_resource'
# bundle install after
ApplicationController:
class ApplicationController < ActionController::Base
include LoadAndAuthorizeResource
end
PagesController
class PagesController < ApplicationController
load_and_authorize_resource
....
More Info here Load And Authorize Resource
I am trying to define a namespace as admin and my routes file is the following;
namespace :admin do
root 'base#index'
resources :boats
end
So I have admin folder, and inside I have base_controller.rb;
class Admin::BaseController < ApplicationController
before_action :logged_in_user, :auth_admin
def index
end
end
and I created cars_controller.rb inherited from BaseController;
class Admin::CarsController < BaseController
def index
#cars = Car.all
end
end
So, like this I get error from the console;
ActionController::RoutingError (uninitialized constant BaseController):
app/controllers/admin/cars_controller.rb:1:in `<top (required)>'
If I change instead of BaseController to ApplicationController, it works without a problem. Bur I could not figured it out why it gives such error.
ActionController::RoutingError (uninitialized constant
BaseController):
Your BaseController is inherited from Admin, so you need to write Admin::BaseController instead of BaseController
class Admin::CarsController < Admin::BaseController
I have a simple website with password protected admin area. It works fine in development. When I upload to Heroku I get the following error:
NameError (uninitialized constant ApplicationController::ADMIN_USERNAME):
My application controller:
class ApplicationController < ActionController::Base
protected
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == ENV[ADMIN_USERNAME] && password == ENV[ADMIN_PASSWORD]
end
end
end
Admin/index_controller:
class Admin::IndexController < ApplicationController
before_filter :authenticate
def index
end
end
I have set heroku env varibale with:
heroku config:add ADMIN_USERNAME:'myusername'
I'm not able to find what to do next.
You need to use ENV['ADMIN_USERNAME'], otherwise the app thinks it's a constant name. Altough it's weird that this works in development.