`
hot88zh
  • 浏览: 182952 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

use current_user in Model

阅读更多

我所有的项目几乎都用 restful_authentication 做了用户管理这部分,应用中经常要在 model 中调用 current_user,而这个是 session 相关的信息,在 Model 中不应直接读到,Google 到一个比较不错的办法 ,是 Beast 的作者提供的。

首先在 User Model 中加入一个类变量:

class User < ActiveRecord::Base
  cattr_accessor :current_user
  ...
end

之后在 ApplicationController 中用 before_filter 给刚才的类变量赋值:

class ApplicationController < ActionController::Base
  include AuthenticatedSystem
  before_filter { |c| User.current_user = c.current_user }
end

看似完美,测试报错,说 current_user 方法是 protected,不能调用。看看 lib/authenticated_system.rb,果然 current_user 方法是 protected,为了不破坏 restful_authenticated plugin 的结构,对 controller 里面的方法做个修改:

class ApplicationController < ActionController::Base
  include AuthenticatedSystem
  before_filter :set_current_user

  protected
  def set_current_user
    User.current_user = self.current_user
  end
end
分享到:
评论

相关推荐

    SSD7 选择题。Multiple-Choice

    In an ER model, which of the following is true about a component attribute? (a) A component attribute is always atomic. (b) Component attributes must always be combined by an aggregation operation...

    netWindows_0.3.0_pre2

    LICENSOR WARRANTS THAT THE COPYRIGHT IN AND TO THEORIGINAL WORK IS OWNED BY THE LICENSOR OR THAT THE ORIGINAL WORK ISDISTRIBUTED BY LICENSOR UNDER A VALID CURRENT LICENSE FROM THE COPYRIGHTOWNER....

    svm matlab版本

    matlab&gt; [predict_label_P, accuracy_P, dec_values_P] = svmpredict(test_label, [(1:120)', test_data*train_data'], model_precomputed); matlab&gt; matlab&gt; accuracy_L % Display the accuracy using linear ...

    UG6.0快捷键大全

    POPUP_MESSAGE Edits parameter values of the selected feature in the current model state. ACTIONS STANDARD BUTTON UG_EDIT_FEATURE_DIMENSION LABEL Feature Dimension... MESSAGE Edits the selected ...

    a project model for the FreeBSD Project.7z

    [2] , ranging from web servers to games, programming languages and most of the application types that are in use on modern computers. Ports will be discussed further in the section The Ports ...

    Android代码-Caravel

    In it's current state, it's far from finished, let alone readable. However, things it contains at the moment are: Texture Atlas Model/Texture loading mechanism, with possibility for themes Mesh ...

    php.ini-development

    If you use constants in your value, and these constants belong to a ; dynamically loaded extension (either a PHP extension or a Zend extension), ; you may only use these constants *after* the line ...

    DE2 web serve的源代码

    2) To offset addresses so that the entire memory model is flat. (Every master sees every slave at the same address). * descriptor_offset_bridge - The rest of the components on the slow_...

    Android代码-DropDownMenu

    DropDownMenu This is a DropDownMenu with the ...use FilterCheckedView which implements Checkable, so you can use selector to respond to all user action. use FilterUrl to save the current choosen data,

    ADS库文件调用说明_V2004AP0105

    This current release of the RF High Power Products Design Kit for ADS v2004a has been implemented as follows: • The Library is implemented as an Agilent® EEsof® EDA Advanced Design System &#40;ADS&...

    Research Advances in Cloud Computing-Springer(2017).pdf

    Today, for almost all the sectors in the world, cloud computing is synonym to on-demand provisioning and delivery of IT services in a pay-as-you-go model. The success story of cloud computing as a ...

    C 语言编缉神经网络工具

    use the compact model. To run the batchnet program, you must specify the run file that it will use. Demo.run is the run file for the demo.bat demonstration. Look at the demo.bat and demo.run ...

    asp.net mvc

    The script-mapping script is not needed if you use the Visual Studio Development Server or if you use IIS 7 in Integrated mode. The scripts are available as a separate unsupported download on the ...

    Turbo C 2.00[DISK]

    o Because of the limited memory available in the Tiny model, it no longer supports graphics functions. o The Version 1.5 graphics drivers (*.BGI) are not compatible with the Version 2.0 graphics ...

    Introduction to Parallel Processing: Algorithms and Architectures

    to use fundamental theories being developed in this area to gain performance and ease-of-use benefits from simpler circuits; to understand the interplay between technological capabilities and ...

    Turbo C 2.01[DISK]

    o Because of the limited memory available in the Tiny model, it no longer supports graphics functions. o The Version 1.5 graphics drivers (*.BGI) are not compatible with the Version 2.01 graphics...

    ARM® Compiler v5.06 for µVision® armasm User Guide

    8.13 When to use flush-to-zero mode in VFP 8.14 The effects of using flush-to-zero mode in VFP 8.15 VFP operations not affected by flush-to-zero mode 8.16 VFP vector mode 8.17 Vectors in the VFP ...

    基于android平台的大象挪盒子游戏设计与开发-毕业论文.doc

    In the first part of the thesis, the current status and research on Android platform game development are briefly outlined. A detailed step-by-step guide is presented on setting up the Android game ...

Global site tag (gtag.js) - Google Analytics