1. we will first finish the create action:
def create user = User.authenticate(params[:session][:email], params[:session][:password]) if user.nil? flash.now[:error] = "Invalid email/password combination." @title = "Sign in" render 'new' else sign_in user redirect_to user end end
for the below, we will write sign_in method!!!
2. start from TDD again!!!
describe SessionsController do . . . describe "POST 'create'" do . . . describe "with valid email and password" do before(:each) do @user = Factory(:user) @attr = { :email => @user.email, :password => @user.password } end it "should sign the user in" do post :create, :session => @attr # Fill in with tests for a signed-in user. end it "should redirect to the user show page" do post :create, :session => @attr response.should redirect_to(user_path(@user)) end end end end
3. in this section, we will need some methods that are need to in both controller and view,
for view, we can define the method in SessionHelpr, (the method in all helpers are viewable for all views.)
to make controller see this method, we just need to include this module into the controller.
class ApplicationController < ActionController::Base protect_from_forgery include SessionsHelper end
4. session and cookies:
Because HTTP is a stateless protocol, web applications requiring user signin must implement a way to track each user’s progress from page to page. One technique for maintaining the user signin status is to use a traditional Rails session (via the special session function) to store a remember token equal to the user’s id:
session[:remember_token] = user.id
User.find_by_id(session[:remember_token])
module SessionHelper def sign_in(user) cookies.permanent.signed[:remember_token] = [user.id, user.salt] self.current_user = user end end
cookies[:remember_token] = {:value => user.id, :expires => 20.years.from_now.utc}
User.find_by_id(cookies[:remember_token])
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
redirect_to current_user
it "should sign the user in" do post :create, :session => @attr controller.current_user.should == @user controller.should be_signed_in end
controller.should be_signed_in
controller.signed_in?.should be_true
self.current_user = user
def current_user=(user) @current_user = user end
def current_user @current_user end
attr_accessor :current_user
def current_user @current_user ||= user_from_remember_token end private def user_from_remember_token User.authenticate_with_salt(*remember_token) end def remember_token cookies.signed[:remember_token] || [nil, nil] end end
def foo(bar, baz) bar + baz end foo(*[1,2]) ====> 3
cookies.signed[:remember_token] || [nil, nil]
def self.authenticate_with_salt(id, cookie_salt) user = find_by_id(id) (user && user.salt == cookie_salt) ? user : nil end
this is very traditional rails code, you really should get used to it!!!
d. we still need to define the signed_in method.
def signed_in?
!current_user.nil?
end
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 799the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8491. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9011. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8671.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7351. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8851. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6921. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10941. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 723in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 762in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 644again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6961. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 724whew!!!, last chapter is a long ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 712There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 652start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 639a session is a semi-permanent c ... -
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 707in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 772Chapter 8.3 this part, we will ...
相关推荐
openssh-9.3p1-1.el7.src.rpm openssh-9.3p1-1.el7.x86_64.rpm openssh-askpass-9.3p1-1.el7.x86_64.rpm openssh-askpass-gnome-9.3p1-1.el7.x86_64.rpm openssh-clients-9.3p1-1.el7.x86_64.rpm openssh-debuginfo-...
《OpenSSH 9.3p2-el7.x64 RPM 安装详解》 OpenSSH 是一个用于在不同网络之间安全地传输数据的开源软件套件。它提供了SSH(Secure SHell)协议的实现,包括客户端和服务端。在这个场景中,我们关注的是OpenSSH 9.3p2...
《OpenSSH 9.3p2 RPM包在openEuler 20.03 LTS-SP2环境中的应用与安全》 OpenSSH是开源社区开发的一款用于网络服务的安全通信工具,它提供了加密的网络连接,使得远程登录和其他网络服务能够在不安全的网络环境中...
《OpenSSH 9.3p2在Red Hat Enterprise Linux 7上的应用与配置详解》 OpenSSH,全称为Open Source Secure Shell,是基于SSH协议的开源实现,为Linux和Unix系统提供安全的远程登录和文件传输功能。OpenSSH 9.3p2是其...
Anolis龙蜥8.6版本 针对openssh漏洞 cve-2020-15778 cve-2023-38480 自制rpm包离线升级openssh由8.0p1到9.3p2 4个安装包之1
scrt-x64-bsafe.9.3.0.2905.exe
《OpenSSH 9.3p2在CentOS 9及Red Hat 9.x上的更新与安全强化》 OpenSSH(Open Source Secure Shell)是广泛应用于Linux和Unix系统中的开源安全套接字层(Secure Socket Layer,SSL)网络协议,提供加密的远程登录和...
AltiumDesignerSummer9Build9.3.1.19182Crack.rar
破解AD09的压缩包仅适用于Build 9.3.1.19182 2.破解已去除标题上的Not signed in 3.局域网内用同一license不再提示冲突 4.仅供学习研究使用,勿用于非法用途。
scrt-sfx-x64-bsafe.9.3.2.2978.exe
《Tina90-TI_zh.9.3.150.328安装包:深入了解Tina软件的应用与安装》 Tina90-TI_zh.9.3.150.328是一款针对电子工程师设计的专业仿真软件,由Texas Instruments(TI)公司开发,旨在为用户提供一个强大的电路分析和...
scrt-sfx-x64-bsafe.9.3.1.2929.exe
Anolis龙蜥8.6版本 针对openssh漏洞 cve-2020-15778 cve-2023-38480 自制rpm包离线升级openssh由8.0p1到9.3p2 4个安装包之4
Centos7 el7.x86_64 官方离线安装包,安装指令为 sudo rpm -ivh glibc-2.17-326.el7_9.3.i686.rpm
scrt-x64-bsafe.9.3.2.2978.exe
EhLib 9.3 Build 9.3.011 Evaluation version. -------------------------------------------- The Library contains components and classes for Borland Delphi versions 7, 9, Developer Studio 2006, Delphi ...
《OpenSSH 9.3p2-1 RPM包在Linux环境中的应用与升级》 OpenSSH,全称为Open Source Secure Shell,是Linux/Unix环境中广泛使用的安全网络协议,用于实现远程登录、文件传输等任务。它提供了加密的安全连接,确保了...
openssh-9.3p2-1.el7.x86_64.rpm openssh-clients-9.3p2-1.el7.x86_64.rpm openssh-debuginfo-9.3p2-1.el7.x86_64.rpm openssh-server-9.3p2-1.el7.x86_64.rpm 等四个文件,下载后移至服务器,使用: yum -y local...
UltraISOV9.3.6.2766 免注册版
Anolis龙蜥8.6版本 针对openssh漏洞 cve-2020-15778 cve-2023-38480 自制rpm包离线升级openssh由8.0p1到9.3p2 4个安装包之3