NameError (uninitialized constant ApplicationController::ADMIN_USERNAME): - ruby-on-rails

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.

Related

Facing the NameError when trying to include concerns in my rails 6 controller

I am trying to create a small project for practising the API Key Authentication using the this tutorial. I have created a concern in following directory of the rails project.
app/controllers/concerns/api_key_authenticable.rb
module ApiKeyAuthenticatable
extend ActiveSupport::Concern
include ActionController::HttpAuthentication::Basic::ControllerMethods
include ActionController::HttpAuthentication::Token::ControllerMethods
attr_reader :current_api_key
attr_reader :current_bearer
# Use this to raise an error and automatically respond with a 401 HTTP status
# code when API key authentication fails
def authenticate_with_api_key!
#current_bearer = authenticate_or_request_with_http_token &method(:authenticator)
end
# Use this for optional API key authentication
def authenticate_with_api_key
#current_bearer = authenticate_with_http_token &method(:authenticator)
end
private
attr_writer :current_api_key
attr_writer :current_bearer
def authenticator(http_token, options)
#current_api_key = ApiKey.find_by token: http_token
current_api_key&.bearer
end
end
In my Controller, I am trying to include the concern like this
app/controllers/ApiKeysController.rb
class ApiKeysController < ApplicationController
include ApiKeyAuthenticatable
# Require token authentication for index
prepend_before_action :authenticate_with_api_key!, only: [:index]
# Optional token authentication for logout
prepend_before_action :authenticate_with_api_key, only: [:destroy]
def index
end
def create
end
def destroy
end
end
But when I run the server and visit the index action of the controller, I get the following error
NameError (uninitialized constant ApiKeysController::ApiKeyAuthenticatable):
app/controllers/ApiKeysController.rb:10:in `<class:ApiKeysController>'
app/controllers/ApiKeysController.rb:2:in `<main>'
Can anybody help me out to fix this issue?
Author here. Your concern’s filename is authenticable but your module is authenticatable. You’ll need to correct the typo in the concern filename.

Reseting session based on user agent and/or ip change. Rails with Devise

I would like to reset session whenever user agent and/or user ip changes. Project uses Rails with Devise for authentication, CanCanCan for authorisation.
My approach is:
class ApplicationController < ActionController::Base
before_action :authorize_ip
def authorize_ip
if warden.authenticated?
warden.session['ip'] ||= request.ip
warden.session['user_agent'] ||= request.user_agent
warden.logout if warden.session['ip'] != 'request.ip' &&
warden.session['user_agent'] != request.user_agent
end
end
end
From my understanding, it should set warden.session['ip'] and user_agent once and then for following requests whenever request['ip'] or user_agent changes, session should be dropped and User should be logged out. However, when tested with different browsers, warden.session['user_agent'] changes according to what browser I use. I suppose I'm misunderstanding something. Also, if there is a better approach to this problem, please share.
Thanks!
I've solved the issue by adding this to initializers/devise.rb
Warden::Manager.after_authentication do |_user, auth, _opts|
auth.raw_session['warden.user.ip'] = auth.request.ip
auth.raw_session['warden.user.user_agent'] = auth.request.user_agent
end
and this to application controller:
class ApplicationController < ActionController::Base
before_action :authorize_ip_user_agent
protected
def authorize_ip_user_agent
return true unless session['warden.user.ip']
warden.logout if session['warden.user.ip'] != request.ip &&
session['warden.user.user_agent'] != request.user_agent
end
end

rails 4 multisite http_basic_authenticate_with

have been trying to solve this for 6 hours now. Hope someone can nail this. I have one codebase running multiple apps on heroku. Some apps already have their own domain. I am already using the host to set locale for each app which is working fine. See below. But authenticating (hide non-ready apps from public) per host doesn't work.
Setting the locale in application controller - working nicely:
before_filter :extract_locale_from_domain
def extract_locale_from_domain
domain = request.host
if domain == 'www.domain.hu'
I18n.locale = :'hu'
elsif domain == 'www.domain.com'
I18n.locale = :'en-US'
else
I18n.locale = :'en-US'
end
end
Now my home page is 'static_pages#home' so first I thought I put the method in the static_pages_controller but that didn't work so I even tried in the application_controller. Even tried to set default URLs per environment (in application_controller) but no luck with that neither (more here). Oh yes, and I tried to restrict per environment with no luck. So I tried several versions this is the one in application_controller (giving nomethod error):
before_filter :authenticate
def authenticate
domain = request.host
if domain == 'www.domain.hu'
http_basic_authenticate_with name: "stuff", password: "boda"
elsif domain == 'www.domain.com'
http_basic_authenticate_with name: "stuff", password: "boda"
else
end
end
This gives the error:
NoMethodError (undefined method `http_basic_authenticate_with' for #
< StaticPagesController:0x000000090d8de8 >):
app/controllers/application_controller.rb:49:in `authenticate'
TIA!
This is what worked:
In applicaiton controller:
before_filter :authenticate
....
protected
def authenticate
domain = request.host
if domain == 'www.domain.hu'
authenticate_or_request_with_http_basic do |username, password| username == 'stuff' && password == 'boda'
end
elsif domain == 'www.domain.com'
authenticate_or_request_with_http_basic do |username, password| username == 'stuff' && password == 'boda'
end
end
end
NoMethodError (undefined method `http_basic_authenticate_with' for #
< StaticPagesController:0x000000090d8de8 >)
This means it can't find the http_basic_authenticate_with method on the StaticPagesController instance.
Try including ActionController::HttpAuthentication::Basic and ActionController::HttpAuthentication::Basic::ControllerMethods modules in your controller and then try to use the http_basic_authenticate_with method again.

heroku uninitialized constant user

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).

Ruby on Rails mime type for mobile error

I'm trying to detect mobile devices with Rails. I'm getting this error : uninitialized constant Mime::MOBILE when I try to access the index page.
mime_types.rb :
Mime::Type.register_alias "text/html", :mobile
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
private
def mobile_device?
if request.user_agent =~ /Mobile|webOS/
request.format = :mobile
return true
end
end
helper_method :mobile_device?
end
I also created a index.mobile.erb file so the user could be redirected to this format.
You must be sure the rails server was launched before the alias added to mime_types.rb. Usualy one is used to see immediately the changes on development enviroment, but in this case, the change was on initializer process.

Resources