Hi i tried to implement forest_admin gem in rails 5 application
When i generate install with this command
rails g forest_liana:install <ENVIRONMENT SECRET>
The logs are alias_method': undefined method current_user' for classApplicationController' (NameError)
My aplication Controller
class ApplicationController < ActionController::Base
skip_before_action :verify_authenticity_token
before_action :session_expirada, unless: :devise_controller?
before_action :set_attr_to_current_user, unless: :devise_controller?
layout :layout_by_resource
alias_method :devise_current_user, :current_user
include RedirectFromEmail
# Pundi Authorization filtros
include Pundit
#after_action :verify_authorized, unless: :devise_controller? , #except: :index
#to catch message error Pundit
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
end
What happend?
You are trying to alias the method ":devise_current_user" to ":current_user". IN your controller current_user is not defined. If you define current_user in your controller(see below) then you will not get this error.
def current_user
#logic to get a handle on current user goes here
end
Remove alias and just use regular devise current_user which is included by default
Related
I am getting uninitialized constant DeviseController which I explicitly require the Devise gem in Rails project rather than using Bundle.require in application.rb
I have the following in application_controller.rb:
require 'devise'
class ApplicationController < ActionController::Base
include Devise::Controllers::Helpers
before_action :configure_permitted_parameters, if: :devise_controller?
But devise_controller? invokes ::DeviseController and for some reason it is not defined. What is going on here?
The use of require in a Rails app is a red flag. It should rarely be used unless you create your own functionality outside of the scope of your MVC (which would usually be placed in the lib directory)...
Assuming you have installed devise...just remove your require and include statements...
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
end
And all should be well if you have a private method named configure_permitted_parameters ie...(with your appropriate custom attributes and action)
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
I followed the instructions http://geekymartian.com/articles/ruby-on-rails-4-salesforce-oauth-implementation/
When I run rake db:migrate I get the following error:
NameError: undefined local variable or method 'current_user' for
TomtomCusu::Application:Class
The method is defined in the application_controller.rg
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
private
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
end
I recently added a piece of code to my ApplicationController to set the timezone of the current block to the one specified by the user.
class ApplicationController < ActionController::Base
around_action :set_time_zone, if: :current_user
protect_from_forgery with: :exception
private
def set_time_zone(&block)
Time.use_zone(current_user.time_zone, &block)
end
end
For some reason when I try to sign in i get a
ActionController::InvalidAuthenticityToken in Devise::SessionsController#create
If i remove
around_action :set_time_zone, if: :current_user
I can sign in and if i add it back after I sign in, everything works as expected.
Any ideas?
This page has good info on the problem, but I was weirdly able to fix this in Rails 5 by putting protect_from_forgery above the around_action/filter. Hope it helps!
I have a simple rails app i had added Active Admin onit, and it worked fine (except that my css is having some problem) but suddenly i started reciving this error just after i tried doing some changes to the dashboard.rb file:
This is the error:
[2016-02-16T18:56:55.902056 #5260] FATAL -- :
ArgumentError (A copy of ApplicationController has been removed from the module
tree but is still active!):
app/controllers/application_controller.rb:26:in `notification'
my application_controller.rb
class ApplicationController < ActionController::Base
include PublicActivity::StoreController
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
skip_before_filter :verify_authenticity_token
helper_method :mailbox, :conversation
before_action :set_last_seen_at, if: proc { user_signed_in? }
before_action :notification, if: proc { user_signed_in? }
private
def mailbox
#mailbox ||= current_user.mailbox
end
def conversation
#conversation ||= mailbox.conversations.find(params[:id])
end
def set_last_seen_at
current_user.update_attribute(:last_seen_at, Time.now)
end
def notification
#shipment_ids = Shipment.where(:user_id => current_user.id).pluck(:id)
#comment = Comment.where("shipment_id IN (?)", #shipment_ids).where(:is_read => false).where.not(:user_id => current_user.id)
end
end
I am following the instructions here in order to add two parameters to my sign up / registration form in a Ruby on Rails + devise based application: http://devise.plataformatec.com.br/#getting-started/strong-parameters
My controller is straight forward:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
before_filter :authenticate_user!
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end
end
The error I'm getting is the following:
NoMethodError in Devise::SessionsController#new
undefined method `<<' for {}:ActionController::Parameters
Am I using the wrong version of Devise or something? I have gem 'devise', :git => 'https://github.com/plataformatec/devise', :branch => 'v3.0' specified in my Gemfile, nothing else is out of the ordinary.
This feature requires Devise 3.1.0.rc according to an issue opened on the Devise repo.