`
peryt
  • 浏览: 55267 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

9.1 sessions

 
阅读更多

a session is a semi-permanent connection between 2 computers, such as client running browser & server running rails.

 

there are several model for session behaviors:

1. forget session on browser close

2. use a optional "remember me" checkbox for persistent sessions.

3. remember the session forever until user explicitly sign out.

4. expire session after a certain amount of time.(this way is especially good on site containing sensitive info, like bank)

 

in this chapter, we will use 3, clear the session until user explicitly sign out.

 

it is convenient to model sessions as RESTful resources:

we will have a signin page for new session.

signin will create a session.

signout will destroy a session.

 

so we need a sessions controller, with new, create, destroy actions.

 

in users controller, we store data into database through user model, to persist data.

here for session, the data will be store in cookie, which is small piece of text places on browser.

so to do signin, we will build the cookie based authentication machinery.

 

1. sessions controller.

 

a. 

rails g controller Sessions new

b.

rm -rf spec/views
rm -rf spec/helpers

 

c. let's create a new file sessions_controller_spec.rb to test sessions controller:

describe SessionsController do
  render_views
  
  describe "GET 'new'" do
    it "should be successful" do
      get 'new'
      response.should be_success
    end
    it "should should have the right title" do
      get 'new'
      response.should have_selector("title", :content => "Sign in")
    end
  end
end

 to get this work, we need to add routes.

 

SampleApp::Application.routes.draw do
  resources :users
  resources :sessions, :only => [:new, :create, :destroy]

  match '/signup',  :to => 'users#new'
  match '/signin',  :to => 'sessions#new'
  match '/signout', :to => 'sessions#destroy'
  .
end

 a. resources :session and take a second argument, :only, to indicate which actions it include.

b. now, we have three named routes:

signin_path  ========> sessions#new

signout_path ========> sessions#destroy

sessions_path =========>sessions#create

 

2. the next step is the sign in form:

<h1>Sign in</h1>

<%= form_for(:session, :url => sessions_path) do |f| %>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="actions">
    <%= f.submit "Sign in" %>
  </div>
<% end %>

<p>New user? <%= link_to "Sign up now!", signup_path %></p>

you may remember we use

 

form_for @user do |f|

 

in prior chapter, but here, we don't session model, so we have to give more info:

 

<%= form_for(:session, :url => sessions_path) do |f| %>
here, we tell rails the resources name and the url.

now you will get this information in the param being submitted:

params[:session]
params[:session][:email]
params[:session][:password]

next we will handle this submission.


 

分享到:
评论

相关推荐

    INFORMATICA POWERCENTER 9.1 英文教程

    Informatica Powercenter 9.1 PDF 教程(英文版),包括 IDP Developer Guide: This document talks about the application programming interfaces (APIs) that enable you to embed data integration ...

    C#教程-unprotected

    **9.1 面向对象的基本概念** - **封装:** 将数据和行为组合在一起,对外部隐藏实现细节。 - **继承:** 子类可以继承父类的属性和方法。 - **多态:** 同一个行为具有多种不同的表现形式。 **10.1 类的声明** - ...

    Drools4.0官方使用手册中文

    2.5.4. WorkingMemory 和有状态/无状态Sessions 33 2.5.5. StatefulSession 38 2.5.6. StatelessSession 40 2.5.7. Agenda 41 2.5.8. Truth Maintenance with Logical Objects 44 2.5.9. 事件模型(Event Model) 48...

    Servlets和JSP核心技术 卷2(英文版) 第一部分

    Section 9.1. Installation of JSTL Section 9.2. c:out Tag Section 9.3. c:forEach and c:forTokens Tags Section 9.4. c:if Tag Section 9.5. c:choose Tag Section 9.6. c:set and c:remove Tags Section ...

    Servlets和JSP核心技术 卷2(英文版) 第二部分

    Section 9.1. Installation of JSTL Section 9.2. c:out Tag Section 9.3. c:forEach and c:forTokens Tags Section 9.4. c:if Tag Section 9.5. c:choose Tag Section 9.6. c:set and c:remove Tags Section ...

    How Tomcat Works: A Guide to Developing Your Own Java Servlet Container

    9.2 Sessions 62 9.2.1 Session接口 62 9.2.2 StandardSession类 63 9.2.3 StandardSessionFacade类 65 9.3 Manager 65 9.3.1 Manager接口 66 9.3.2 ManagerBase类 66 9.3.3 StandardManager类 67 9.3.4 ...

    Artech House - SMS and MMS Interworking in Mobile Networks

    9.1 Introduction 145 9.2 Standard Model for MMS Sending and Receiving 145 9.2.1 MMS Relay/Server 145 9.2.2 MMS User Databases 145 9.2.3 MMS User Agent 146 9.2.4 MMS VAS Applications 146 9.3 Standard ...

    Hibernate Reference Documentation3.1

    2.5. Contextual Sessions 3. Configuration 3.1. Programmatic configuration 3.2. Obtaining a SessionFactory 3.3. JDBC connections 3.4. Optional configuration properties 3.4.1. SQL Dialects 3.4.2. Outer ...

    UNIX环境高级编程(第二版,英文版)

    Section 9.1. Introduction Section 9.2. Terminal Logins Section 9.3. Network Logins Section 9.4. Process Groups Section 9.5. Sessions Section 9.6. Controlling Terminal ...

    Windows2000活动目录开发人员参考库第4卷ADSI参考手册

    12.2.2 IADsFileService Operations::Sessions 方法 12.3 IADsPrintJobOperations接口 12.3.1 IADsPrintJobOerations接口的 特性方法 12.3.2 IADsPrintJobOperations::Pause 方法 12.3.3 ...

    UNIX环境高级编程英文第三版+源码

    9.1 Introduction 285 9.2 Ter minal Logins 285 9.3 Networ k Logins 290 9.4 Process Groups 293 9.5 Sessions 295 9.6 Controlling Terminal 296 9.7 tcgetpgrp, tcsetpgrp, and tcgetsid Functions 298 9.8 Job ...

    SpringSecurity 3.0.1.RELEASE.CHM

    9.1. BasicAuthenticationFilter 9.1.1. 配置 9.2. DigestAuthenticationFilter 9.2.1. Configuration 10. Remember-Me认证 10.1. 概述 10.2. 简单基于散列标记的方法 10.3. 持久化标记方法 10.4. Remember-...

    portal 开发指南

    ##### 9.1 正常WindowState 这是Portlet的默认状态,通常指Portlet在一个标准大小的窗口中显示。 ##### 9.2 最大化WindowState 当Portlet处于最大化状态时,它会占据整个屏幕或者Portal页面的最大可用空间。 ###...

    ORACLE9i_优化设计与系统调整

    §9.1 项目分析要点考虑 95 §9.1.1 对应用系统类型的认识 95 §9.1.2 软件项目计划 95 §9.1.3 开发环境资源的配置 96 §9.1.4 各种人员的招募要求 96 §9.1.5 开发组工作的开始 96 §9.2 应用系统运行环境分析 96 ...

    resin-reference-3.0.13文档

    ##### 9.1 服务器缓存 (Server Caching) - **服务器缓存配置**:提供了如何配置 Resin 的服务器缓存以提高性能。 ##### 9.2 JVM 调优 (JVMTuning) - **JVM 调优技巧**:提供了如何优化 JVM 以提高 Resin 的运行...

    hibernate 教程

    配置,会话和工厂(Configurations, Sessions and Factories) 10.2. 线程和连接(Threads and connections) 10.3. 考虑对象辨别 10.4. 乐观并发控制(Optimistic concurrency control) 10.4.1. 使用...

    hibernate

    配置,会话和工厂(Configurations, Sessions and Factories) 10.2. 线程和连接(Threads and connections) 10.3. 考虑对象辨别 10.4. 乐观并发控制(Optimistic concurrency control) 10.4.1. 使用...

    SIP rfc3261 协议下载

    9.1 ClientBehavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 9.2 ServerBehavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

Global site tag (gtag.js) - Google Analytics