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| %>
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 814the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8521. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9091. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8751.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7441. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8931. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6961. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11031. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 732in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 768in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 655again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7041. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 730whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7411. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 722There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 655start from TDD!!! 1. requir ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 714in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 775Chapter 8.3 this part, we will ...
相关推荐
Informatica Powercenter 9.1 PDF 教程(英文版),包括 IDP Developer Guide: This document talks about the application programming interfaces (APIs) that enable you to embed data integration ...
**9.1 面向对象的基本概念** - **封装:** 将数据和行为组合在一起,对外部隐藏实现细节。 - **继承:** 子类可以继承父类的属性和方法。 - **多态:** 同一个行为具有多种不同的表现形式。 **10.1 类的声明** - ...
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...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
12.2.2 IADsFileService Operations::Sessions 方法 12.3 IADsPrintJobOperations接口 12.3.1 IADsPrintJobOerations接口的 特性方法 12.3.2 IADsPrintJobOperations::Pause 方法 12.3.3 ...
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 ...
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-...
##### 9.1 正常WindowState 这是Portlet的默认状态,通常指Portlet在一个标准大小的窗口中显示。 ##### 9.2 最大化WindowState 当Portlet处于最大化状态时,它会占据整个屏幕或者Portal页面的最大可用空间。 ###...
§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 ...
##### 9.1 服务器缓存 (Server Caching) - **服务器缓存配置**:提供了如何配置 Resin 的服务器缓存以提高性能。 ##### 9.2 JVM 调优 (JVMTuning) - **JVM 调优技巧**:提供了如何优化 JVM 以提高 Resin 的运行...
配置,会话和工厂(Configurations, Sessions and Factories) 10.2. 线程和连接(Threads and connections) 10.3. 考虑对象辨别 10.4. 乐观并发控制(Optimistic concurrency control) 10.4.1. 使用...
配置,会话和工厂(Configurations, Sessions and Factories) 10.2. 线程和连接(Threads and connections) 10.3. 考虑对象辨别 10.4. 乐观并发控制(Optimistic concurrency control) 10.4.1. 使用...
9.1 ClientBehavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 9.2 ServerBehavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....