`
CaiDeHen
  • 浏览: 89577 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

#119 Session Based Model

阅读更多
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 %>
分享到:
评论

相关推荐

    论文研究-A State Presence Implementation Model in SIP-based Applications.pdf

    一种基于SIP的状态呈现实现模型,邝坚,王贵豹,SIP协议是一种用于两方或多方之间创建、修改和终结会话的应用层控制(信令)协议。基于SIP协议,人们创造出各种前所未有的应用,包

    rankingSvm

    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 后量子加密讲义

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

    29502-g00_SMF功能接口介绍.docx

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

    Java.EE.7.Essentials

    Object Model API Chapter 7 : WebSocket Annotated Server Endpoint Programmatic Server Endpoint Annotated Client Endpoint Programmatic Client Endpoint JavaScript WebSocket Client Encoders...

    标准pso代码-鲁.zip_On Strategy_algorithm,pso_cec2013_pso for decisio

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

    DMS_PSO.zip_On Strategy_algorithm_dms_dms-pso_dynamic pso

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

    PSO.zip_On Strategy_algorithm_article_cec2013_pso dynamic

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

    ERwin方法论

    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 信息建模...

    动网格边界设置 详细讲述了fluent中动网格边界的设置情况

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

    nat穿透--ice--rfc5245

    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).

    Apache Axis2 Web Services, 2nd Edition.pdf

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

    i-vector的工具箱

    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]...

    Artech House - SMS and MMS Interworking in Mobile Networks

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

    Java邮件开发Fundamentals of the JavaMail API

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

    php.ini-development

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

    外文翻译 stus MVC

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

    sigmod2011全部论文(2)

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

Global site tag (gtag.js) - Google Analytics