我所有的项目几乎都用 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
分享到:
相关推荐
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...
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....
matlab> [predict_label_P, accuracy_P, dec_values_P] = svmpredict(test_label, [(1:120)', test_data*train_data'], model_precomputed); matlab> matlab> accuracy_L % Display the accuracy using linear ...
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 ...
[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 ...
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 ...
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 ...
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_...
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,
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 (ADS&...
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 ...
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 ...
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 ...
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 ...
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 ...
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...
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 ...
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 ...