views:

163

answers:

2

Hello! At the moment, I am creating some kind of admin panel/backend for my site. I want to do the following: Only admins (a user has a user_role(integer) --> 1 = admin, 2 = moderator, 3 = user) can see and access a link for the admin panel. So I created an admin_controller. In my admin controller I created a new function called 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 applicaton.html.erb) I created the link:

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

Now I get an error called:

undefined method `is_admin?'

Where is the problem? Please help me solving this problem!

Okay, sorry for this, but it still wont 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>

That's it. Any ideas?

Btw: now the error changed a bit. Now it is:

undefined method `is_admin?' for nil:NilClass

My Session Create (in sessions_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

+7  A: 

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

helper_method :is_admin?

However, in your case I would suggest that you move this method into the user 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 the view,

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

Oh, and btw, make sure that your users cannot change their roles arbitrarily via mass attributes assignment. And also it would be better to define constants for those role integer values. Sorry if this is too obvious :)

neutrino
Thanks, but I wont work...now I moved the is_admin? method in my users_controller and wrote helper_method :is_admin? on top of my controller:class UsersController < ApplicationController layout 'application' # Be sure to include AuthenticationSystem in Application Controller instead include AuthenticatedSystem helper_method :is_admin? def is_admin? self.user_role == 1 end...But I still get the same error: undefined method `is_admin?'Writing it to the admin_controller, I get the error because of user_role. I think cause of current_user is not defined.
Newbie
You are messing different things here. Probably my code didn't work because you don't have access to `current_user` in your views. But from the code you posted I assume it's available in the controllers, so put the `is_admin?` method back into the `User` class and declare `helper_method :current_user` in the `ApplicationController`. If this still doesn't work, try posting the code of your controllers and probably we'll figure something out.
neutrino
I observed your code. First, you don't need to `helper_method :is_admin?`. Second, double-check that `@current_user` really isn't `nil`. I think it is.
neutrino
I am new to rails. Now I removed the helper_method :is_admin? from my users_controller.rb.in my view I wrote: <% if current_user != nil %> <%= link_to "Admin Panel", :admin_panel unless current_user.is_admin? %> <% end %>(How can I add code tags for stackoverflow in my comments?)Now, the error appears after login. In my understanding, after login there is a current_user, right? Where do I have to double check for current_user?Thank you so much for helping me!
Newbie
what kind of error? to check the user, add a line `puts "*" * 10 + @current_user` to the bottom of your `current_user` method and check the logs.the code in comments can be marked by backticks (```), but there's no way to do line breaks, so it's better to add larger code fragments to your answer.
neutrino
Okay, now I included the output. It is: `********** ` for `puts "*" * 10 + @current_user.to_s`. without .....to_s I get the error: `can't convert nil into String` So it seem current_user is nil. But why? In my Question I will add my session create (login).
Newbie
Newbie
+2  A: 

use helper_method if you want to use your controller's 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
Salil