Undefined method logged_in? Ruby on Rails - ruby-on-rails

I can't make head or tail of this (but I'm probably being an idiot). I have a class ApplicationController that contains the following method:
def logged_in?
!current_user.nil?
end
if logged_in?
load_and_authorize_resource :unless => :devise_controller?, :except => :show
end
This gives me an error:
undefined method `logged_in?' for ApplicationController:Class
This method is clearly defined, how is it coming back as undefined?

Maybe the best place to declare it is in /helpers/application_helper.rb instead of application_controller.
Also, to verify if a user is signed in, Devise provides the following helper user_signed_in?.
Source: https://github.com/plataformatec/devise

I think you forgot to write:
helper_method :logged_in?
in ApplicationController.

Related

Rails current_user nil

I have a controller, in this controller #current user is OK in show, new, create, but doesn't work in def _params, for example, #current_user.role:
undefined method `role' for nil:NilClass.
Thanks.
def company_params
if #current_user.role.name != 'admin'
params[:company_id] = #current_user.company.id
end
params.require(:company).permit(.........)
end
ERROR: undefined method `role' for nil:NilClass
#current_user is defined in a filter at the begginng but just for the defined actions. The company params doesn't seem to be included in the list. Try adding it.
before_action :set_current_user, only: [:balbalba, :company_params] #might look different

ActionController::RoutingError (undefined method `before_filter' for Class): Error

Basically I have a UsersInitializeController Class
class UsersInitializeController < ApplicationController
before_filter :authenticate_user!
def create
render true
end
end
authenticate_user! is found in the Application Controller
class ApplicationController < ActionController::Base
# protect_from_forgery
def authenticate_user!
#current_user = User.find_by_token params[:auth_token]
if !#current_user
#current_user = User.create :token => params[:auth_token]
end
end
end
When my application starts, it sends POST request to the UsersInitializeController. Since before_filter is set, it will thus call authenticate_user! first. However the error I got says before_filter is an undefined method.
From my knowledge, before_filter exist in ActionController, and since UsersInitializeContoller < ApplicationController < ActionController, I shouldn't be getting this error. Has anyone encounter this issue before ?
Exception Stack (as requested)
Started POST "/users_initialize.json" for 127.0.0.1 at 2012-03-06 00:32:50 -0800
ActionController::RoutingError (undefined method `before_filter' for UsersInitializeController:Class):
app/controllers/users_initialize_controller.rb:3:in `<class:UsersInitializeController>'
app/controllers/users_initialize_controller.rb:1:in `<top (required)>'
Routes.rb file (as requested)
MyApplication::Application.routes.draw do
resources :users_initialize
match 'info/required_client_version' => 'info#required_client_version'
end
### Problem Solved ###
Unused Devise Gem somehow causing the complication. Removed it and done.
add the before_filter within an "included do" block:
included do
before_filter :authenticate_user!
end
Update:
just noticed you solved it already. However I was having the same troubles and the solution above solved it in my case. So, I'll leave the comment here since it may help others
Not able to reproduce, the code you posted works fine on my Rails 3.2.2 app.
Probably there is something wrong with your source file (i.e. some extra hidden bytes somewhere).
You could try a step-by-step approach to solve this:
add a new UsersController and add resources :users to routes.rb
add an index action with the following code:
def index
render :text => "Hello there"
end
When you visit http://localhost:3000 you should see the text "Hello there"
Add the before_filter and verify that the filter is executed by adding e.g. logger.warn( 'In the Filter' ) to the beginning of the filter method

Problems with Warden::Manager after_authentication callbacks

This would probably be simpler for me if Ruby was my first language, but anyway, here's my question:
Using Rails 3.1, I'm trying to access some of the Warden Manager callbacks with Devise to create a new 'Cart' each time a user signs in. I'm placing this logic in my ApplicationController. Problem is, when I create a Cart, I want to give it a user id. I've been trying to use Devise's helper method current_user but that isn't working.
Most importantly, I want to know why I can't access my helper methods or methods defined in the ApplicationController from within the Warden::Manager block. But I also want to know how I can edit my code so I can use Devise's current_user method (and my current_cart method, shown below) within the block without errors like the one listed below being called.
Here's my code:
class ApplicationController < ActionController::Base
helper :all
helper_method :current_user
protect_from_forgery
before_filter :fetch_categories
.
.
.
def current_cart
#current_cart ||= Cart.find_by_user_id(current_user.id)
end
Warden::Manager.after_authentication do |user, auth, opts|
Cart.create!(:user_id => current_user.id)
end
end
Here's the error:
NameError in Devise::SessionsController#create
undefined local variable or method `current_user' for ApplicationController:Class
Warden::Manager.after_authentication do |user, auth, opts|
Cart.create!(:user_id => user.id)
end
In the after_authentication block you don't have access to the current_user. Instead, use the newly-authenticated user object passed as a parameter.
Well, I don't really like answering my own questions, but since I feel obligated to leave no question unanswered:
What I ended up doing was essentially side-stepping the whole callback thing altogether. Although this might be idiosyncratic to my situation, here's what I did:
In the application controller:
before_filter :authenticate_user!, :only => :current_cart
That way, a user must be signed in for current_cart to be called. And also change current_cart to:
def current_cart
session[:cart_id] ||= Cart.create(:user_id => current_user.id).id
#current_cart ||= Cart.find(session[:cart_id])
end
So current_cart instantiates a new cart if it doesn't yet exist. You can also do the before_filter stuff in other controllers which might affect your cart, such as LineItems or Products.

Rails 3 helper_method

I have setup a helper method within the application controller ie.
class ApplicationController < ActionController::Base
# Helpers
helper_method :current_user
# Private Methods
private
def current_user
#current_user ||= Tester.find(session[:user_id]) if session[:user_id]
end
end
If I try to access to current_user variable within a view im getting an error
#code
Welcome <%=#current_user.first_name%>
#error
undefined method `first_name' for nil:NilClass
I know the session is good. Is this the correct was to access to current_user ?
Thanks for the help
It's a method, you're trying to access an instance variable, do it like this:
Welcome <%= current_user.first_name%>

Accessing devise helper 'user_signed_in?'

I'm unable to use 'user_signed_in?' in my application controller, and wondered if anyone knew how to fix it.
It's works fine in my views, but in my application controller i get
NoMethodError in PostsController#index
undefined method `user_signed_in?' for ApplicationController:Class
A lot of people had this problem on rail 3.0.3, but I'm using rails 2.3.8. The suggested fix was
to use devise_for :user in your routes.rb but that resulted in
Internal Server Error
undefined method `devise_for' for main:Object
Help would be greatly appreciated
Thanks
I use devise with 2.38
How about having
==> application_controller.rb <==
protected
def authorize
unless User.find_by_id(session[:user_id])
session[:original_uri] = request.request_uri
flash[:notice] = "Please Log In!"
redirect_to :controller => 'admin', :action => 'login'
end
end
end
then each controller, e.g. food_items:
class FoodItemsController < ApplicationController
before_filter :authorize, :except => [:index, :show] # For all methods except these...
# GET /food_items
slightly different approach. Might help.

Resources