If you have a lot of logic associated with the data inside a session, you'll need some central location to put this logic. See how to create a session based model in this episode.
# models/user_session.rb
class UserSession
def initialize(session)
@session = session
@session[:comment_ids] ||= []
end
def add_comment(comment)
@session[:comment_ids] << comment.id
end
def can_edit_comment?(comment)
@session[:comment_ids].include?(comment.id) && comment.created_at > 15.minutes.ago
end
end
# controllers/application.rb
def user_session
@user_session ||= UserSession.new(session)
end
helper_method :user_session
# comments_controller.rb
before_filter :authorize, :only => [:edit, :update]
def create
@comment = Comment.new(params[:comment])
if @comment.save
user_session.add_comment(@comment)
flash[:notice] = "Successfully created comment."
redirect_to article_url(@comment.article_id)
else
render :action => 'new'
end
end
private
def authorize
unless user_session.can_edit_comment? Comment.find(params[:id])
flash[:error] = "You are no longer able to edit this comment."
redirect_to root_url
end
end
<% if user_session.can_edit_comment? comment %>
<p><%= link_to "Edit", edit_comment_path(comment) %></p>
<% end %>
分享到:
相关推荐
一种基于SIP的状态呈现实现模型,邝坚,王贵豹,SIP协议是一种用于两方或多方之间创建、修改和终结会话的应用层控制(信令)协议。基于SIP协议,人们创造出各种前所未有的应用,包
as well as the state-of-the-art Latent Dirichlet Allocation based model (LDA). Specifically, the RankingSVM-based method achieves statistical significant improvements over the SVM-based method and has...
PKC 2019 Beijing day3 session 10 and 11 slides. topic: Post Quantum Cryptography (I) and (II) papers from IACR: Efficiently Masking Binomial Sampling at Arbitrary Orders for Lattice-Based Crypto ...
The present document specifies the stage 3 protocol and data model for the Nsmf Service Based Interface. It provides stage 3 protocol definitions and message flows, and specifies the API for each ...
Object Model API Chapter 7 : WebSocket Annotated Server Endpoint Programmatic Server Endpoint Annotated Client Endpoint Programmatic Client Endpoint JavaScript WebSocket Client Encoders...
Abstract: With the development of engineering technology and the improvement of mathematical model, a large number of optimization problems were developed from low dimensional optimization to large-...
Abstract: With the development of engineering technology and the improvement of mathematical model, a large number of optimization problems were developed from low dimensional optimization to large-...
Abstract: With the development of engineering technology and the improvement of mathematical model, a large number of optimization problems were developed from low dimensional optimization to large-...
7.2.2 The Key Based (KB) Model 54 7.2.3 The Project Information Models 54 7.2.4 The Fully-attributed (FA) Model 54 7.2.5 The Transformation Model 54 7.3 关系系统的DBMS模型 55 7.4 信息建模...
Purpose of this session • Show you how to use the controls for the Dynamic Mesh (DM) model, based on a few examples • Show you how to write simple UDFs that control the dynamic zones, based on a ...
ICE :解决NAT穿透问题的方法 This document describes a protocol for Network Address Translator (NAT) traversal for UDP-...utilizing the offer/answer model, such as the Session Initiation Protocol (SIP).
Chapter 10, Session Management - Go beyond single invocation and learn how to use Axis2 features to provide better and more eficient statefull service. Chapter 11, Developing JAX-WS Web Services - ...
from discrete vector quantization (VQ) based systems to adapted Gaussian mixture model (GMM) solutions, and more recently to factor analysis based Eigenvoice (i-vector) frameworks. The Identity ...
Spring session | 分布式Session管理 | [http://projects.spring.io/spring-session/](http://projects.spring.io/spring-session/) MyBatis | ORM框架 | [http://www.mybatis.org/mybatis-3/zh/index.html]...
8.5.1 Session Management: Transceiver PDUs 138 8.5.2 Message Submission Operation 139 8.5.3 Other SMPP Operations 143 8.6 GSM IS-41 Interworking Through SMPP 143 Reference 144 CHAPTER 9 MMS ...
further than the JavaMail API, which offers a protocol-independent model for working with IMAP, POP, SMTP, MIME, and all those other Internet-related messaging protocols. With the help of the ...
session.hash_bits_per_character ; Default Value: 4 ; Development Value: 5 ; Production Value: 5 ; short_open_tag ; Default Value: On ; Development Value: Off ; Production Value: Off ; track_errors ...
ActionForm bean represents the Model state at a session or request level, and not at a persistent level. The JSP file reads information from the ActionForm bean using JSP tags. • View The view is ...
Changing Flights in Mid-Air: A Model for Safely Modifying Continuous Queries (Page 613) Kyumars Sheykh Esmaili (ETH Zurich) Tahmineh Sanamrad (ETH Zurich) Peter M. Fischer (ETH Zurich) Nesime Tatbul ...