开发者

Rails: def admin_user

开发者 https://www.devze.com 2023-04-02 07:12 出处:网络
How can I define admin_user if I h开发者_如何学JAVAave this code in my controller: def current_user

How can I define admin_user if I h开发者_如何学JAVAave this code in my controller:

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

admin_user must be a user with id 1.

I want to use something like this:

<% if admin_user %> content for admin only< %elsif current_user%> content for admin and other logged in users <% end %>

ps. I don't want to use CanCan or other gems.


def admin_user
  current_user.try(:id) == 1
end

should work but, IMHO, it isn't very nice.


Something like this?

# checks to see if current_user exists, and if so, has id 1
def admin_user?
  current_user && current_user.id == 1
end


Maybe in your user model:

def is_admin?
  self.user_id == 1
end

Then, you can call something like

current_user.is_admin?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号