Question

I have a very frustrating issue.

I can't call any helper method from my views in rails.This is what I have:

ApplicationController.rb

class ApplicationController < ActionController::Base

   protect_from_forgery

   def current_user
     @current_user ||= User.find(session[:user_id]) if session[:user_id]
   end

   helper_method :current_user
   helper_method :all

  end

/app/views/welcome/_navi_bar.haml:

%ul.nav.nav-pills.pull-right
   %li.pull-right
      - if current_user
         %a.btn-small{"data-toggle" => "modal", :href => log_out_path, :role => "button"}
           %i.icon-user.icon-white
           %strong
             Log Out
       - else
         %a.btn-small{"data-toggle" => "modal", :href => "#LoginBox", :role => "button"}
           %i.icon-user.icon-white
            %strong
             Login

This is what I get as error:

undefined local variable or method `current_user' for #<#<Class:0x007ff0a0544668>:0x007ff0a05414b8>

I really don't get what the problem is. Please help !

Was it helpful?

Solution

You have written your code in application controller instead of application helper That is the reason why your method is not getting called

if you want to check if current user is logged in or not you may just use before filter in application controller and call the method

whenever you dont need to check the method add skip before filter in that place

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top