Is_admin? function in rails? - undefined method Error

I am currently creating some sort of admin panel / backend for my site. I want to do the following: Only admins (user has user_role (integer) -> 1 = admin, 2 = moderator, 3 = user) can see and access the link for the admin panel. So I created admin_controller. In my admin controller, I created a new function is_admin ?:

class AdminController < ApplicationController

  def admin_panel
  end

  def is_admin?
    current_user.user_role == 1
  end

end

      

my route looks like.

map.admin_panel '/admin-panel', :controller => 'admin', :action => 'admin_panel'

      

and in my _sidebar.html.erb (partial in applaton.html.erb) I created a link:

<%= link_to "Admin Panel", :admin_panel unless is_admin? %>

      

Now I am getting the error:

undefined method `is_admin?'

      

Where is the problem? Please help me to solve this problem!

Ok, sorry about that, but this still doesn't work. Here are my controllers:

application_controller.rb:

class ApplicationController < ActionController::Base
      include AuthenticatedSystem
      helper :all
      protect_from_forgery

      helper_method :current_user

        def current_user
          @current_user ||= User.find_by_id(session[:user])
        end
end

      

users_controller.rb:

class UsersController < ApplicationController
      layout 'application'

      include AuthenticatedSystem

      helper_method :is_admin? #only added this line

      def new
      end
      ...
end

      

user.rb

 require 'digest/sha1'
    class User < ActiveRecord::Base
        # Virtual attribute for the unencrypted password
        attr_accessor :password
        ... #more stuff but nothing for is_admin?

      def active?
        # the existence of an activation code means they have not activated yet
        activation_code.nil?
      end

      #here is my is_admin? code
      def is_admin?
        self.user_role == 1
      end
      ...
   end

      

and now my view (_sidebar.html.erb):

<div>
    <%= link_to "Admin Panel", :admin_panel unless current_user.is_admin? %>
</div>

      

What is it. Any ideas?

Btw: the bug has changed a bit now. Now this:

undefined method `is_admin?' for nil:NilClass

      

My Session Create (in session_controller.rb):

  def create
self.current_user = User.authenticate(params[:login], params[:password])
if logged_in?
  if params[:remember_me] == "1"
    current_user.remember_me unless current_user.remember_token?
    cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
  end
  redirect_back_or_default('/')
  flash[:notice] = "Logged in successfully"
else
  render :action => 'new'
end

      

end

+2


a source to share


2 answers


The problem is that the methods defined in your controllers are not available in your views, unless you do this in your controller:

helper_method :is_admin?

      

However, in your case, I would suggest that you move this method to a custom model, as it seems to be more or less part of the business logic of the application.

So in your user model



class User < ActiveRecord::Base

  def is_admin?
    self.user_role == 1
  end
end

      

And then in view

<%= link_to "Admin Panel", :admin_panel unless current_user.is_admin? %>

      

Oh, and btw, make sure your users can't change their roles arbitrarily through assigning mass attributes. And also it would be better to define constants for these integer role values. Sorry if this is too obvious :)

+11


a source


use helper_method if you want to use your controller method in your views



class AdminController < ApplicationController

  helper_method :is_admin?  #you should include this line so that you can access it in your view.

  def admin_panel
  end

  def is_admin?
    current_user.user_role == 1
  end

end

      

+2


a source







All Articles