ruby-on-rails - 是_admin?在 rails 中运行? - 未定义的方法错误

标签 ruby-on-rails

目前,我正在为我的站点创建某种管理面板/后端。 我想做以下事情: 只有管​​理员(用户具有 user_role(integer) --> 1 = admin,2 = moderator,3 = user)可以查看和访问管理面板的链接。 所以我创建了一个 admin_controller。在我的管理 Controller 中,我创建了一个名为 is_admin? 的新函数:

class AdminController < ApplicationController

  def admin_panel
  end

  def is_admin?
    current_user.user_role == 1
  end

end

我的路线是这样的。

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

在我的 _sidebar.html.erb(部分在 applicaton.html.erb 中)我创建了链接:

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

现在我收到一个错误:

undefined method `is_admin?'

问题出在哪里?请帮我解决这个问题!

好的,对此感到抱歉,但它仍然无法正常工作。这是我的 Controller :

应用程序 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

用户 Controller .rb:

class UsersController < ApplicationController
      layout 'application'

      include AuthenticatedSystem

      helper_method :is_admin? #only added this line

      def new
      end
      ...
end

用户.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

现在我的观点 (_sidebar.html.erb):

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

就是这样。有什么想法吗?

顺便说一句:现在错误有所改变。现在是:

undefined method `is_admin?' for nil:NilClass

我的 session 创建(在 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

结束

最佳答案

问题是你的 Controller 中定义的方法在你的 View 中不可用,除非你在 Controller 中这样做:

helper_method :is_admin?

但是,对于您的情况,我建议您将此方法移动到用户模型中,因为它似乎或多或少是应用程序业务逻辑的一部分。

因此,在您的用户模型中,

class User < ActiveRecord::Base

  def is_admin?
    self.user_role == 1
  end
end

然后在 View 中,

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

哦,顺便说一句,确保您的用户不能通过批量属性分配任意更改他们的角色。而且最好为那些角色整数值定义常量。对不起,如果这太明显了:)

关于ruby-on-rails - 是_admin?在 rails 中运行? - 未定义的方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2763979/

相关文章:

ruby-on-rails - 在 Ruby On Rails 中使用seeds.rb 中的方法

ruby-on-rails - rails 3 基于多个字段查找或创建

ruby-on-rails - 如何在 Active Storage 测试中 stub 文件大小? (测试::单位)

ruby-on-rails - rails : How to get enum value for joined table?

ruby-on-rails - 如何在一天中显示问候语

ruby-on-rails - Rails上传文件到ftp服务器

ruby-on-rails - 为什么 ActiveRecord 的自动保存对我的关联不起作用?

ruby-on-rails - 有人可以解释这个 find 方法的行为吗?

ruby-on-rails - 使用 ActiveRecord 总和进行排序和限制?

ruby-on-rails - 如何使用 Ruby 或 Rails 从 URL 中提取 URL 参数?