【问题标题】:Rails 3.1 helper-method to check values of current_userRails 3.1 helper-method 检查 current_user 的值
【发布时间】:2011-09-12 10:32:56
【问题描述】:

我已经基于http://ruby.railstutorial.org/ruby-on-rails-tutorial-book 和 RailsCast 274 (http://railscasts.com/episodes/274-remember-me-reset-password) 中提供的授权使用 Rails 3.1 构建了自己的基本博客工具。所以我有一个 current_user 用于我的 Rails 应用程序中的会话管理,如下所示:

def current_user
  @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
end

现在我遇到了问题,我想检查这个用户的两个值。

我的用户模型如下所示:

# == Schema Information
#
# Table name: users
#
#  id              :integer         not null, primary key
#  name            :string(255)
#  email           :string(255)
#  password_digest :string(255)
#  admin           :boolean         default(FALSE)
#  created_at      :datetime
#  updated_at      :datetime
#  auth_token      :string(255)
#  writer          :boolean         default(FALSE)
#  website         :string(255)
#

class User < ActiveRecord::Base
  attr_accessible :name, :email, :website, :password, :password_confirmation
  has_secure_password

  has_many :articles, :dependent => :destroy

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  website_regex = /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix

  validates :name, :presence => true,
                   :length   => { :maximum => 50 }
  validates :email, :presence => true,
                    :format   => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false }
  # Automatically create the virtual attribute 'password_confirmation'.
  validates :password, :presence     => true,
                       :confirmation => true,
                       :length       => { :within => 6..40 }
  validates :website, :format => { :with => website_regex }

  before_create { generate_token(:auth_token) }

  private              
    def generate_token(column)
      begin
        self[column] = SecureRandom.urlsafe_base64
      end while User.exists?(column => self[column])
    end
end

我要检查的值是 :admin:writer,无论它们是否为真。如您所见,它们未标记为可访问,因为我希望只有管理员才能编辑这些值。我在 RailsCast 237 中解决了这个问题(我想插入链接,但作为新用户,我不允许发布两个以上的链接)。使用 current_user.admin?current_user.writer? 在控制器中检查这些参数似乎没有问题。但是,如果在视图中尝试此操作,我会收到以下错误消息:

ActionView::Template::Error (undefined method `writer' for nil:NilClass):
    15:       <li><%= link_to "Log in", signin_path %></li>
    16:       <% end %>
    17:     </ul>
    18:     <% if current_user.writer == true %>
    19:     <ul>
    20:       <li><%= link_to "new article", newarticle_path %></li>
    21:     </ul>
  app/views/layouts/_header.html.erb:18:in `_app_views_layouts__header_html_erb___3376819447841576130_36475600'
  app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__676755681270806253_35068900'

请有人告诉我这个问题是否可以解决以及如何解决?非常感谢!

更新 1: 我发现 current_user.admin?current_user.writer? 在控制器中也不起作用。所以看来我需要一个通用的辅助方法。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 railscasts


    【解决方案1】:

    这个错误意味着你没有登录。'current_user'变量中有注释。

    您需要添加另一个检查用户是否登录。

    所以试试这个。

    <% if current_user %>
      <% if current_user.writer %> 
      <% end %>
      #this will return true no need for doing current_user.writer == true
    <% end %>
    

    希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      相关资源
      最近更新 更多