9.3 sign in success.
- 博客分类:
- ruby and rails
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 739the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8061. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 8781. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8371.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7041. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8591. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6481. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10641. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 689in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 731in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 604again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6631. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 683whew!!!, last chapter is a long ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 671There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 605start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 612a 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 675in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 734Chapter 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-...
openssh9.3p2-el7.x64 rpm安装包
openssh 9.3p2 for redhat el7
scrt-x64-bsafe.9.3.0.2905.exe
SecureCRT9.3.0.2905SecureCRT9.3.0.2905SecureCRT9.3.0.2905SecureCRT9.3.0.2905
基于openEuler 20.03 (LTS-SP2) Linux环境制作的openssh-9.3p2版本的RPM包,适用于openssh低版本漏洞修复
Anolis龙蜥8.6版本 针对openssh漏洞 cve-2020-15778 cve-2023-38480 自制rpm包离线升级openssh由8.0p1到9.3p2 4个安装包之4
破解AD09的压缩包仅适用于Build 9.3.1.19182 2.破解已去除标题上的Not signed in 3.局域网内用同一license不再提示冲突 4.仅供学习研究使用,勿用于非法用途。
scrt-sfx-x64-bsafe.9.3.1.2929.exe
AltiumDesignerSummer9Build9.3.1.19182Crack.rar
EhLib 9.3 Build 9.3.027.rar
scrt-x64-bsafe.9.3.2.2978.exe
这是Tina90-TI_zh.9.3.150.328的安装包,可以直接安装使用,大家下载安装后有什么问题可以随时联系,欢迎交流!!!
Anolis龙蜥8.6版本 针对openssh漏洞 cve-2020-15778 cve-2023-38480 自制rpm包离线升级openssh由8.0p1到9.3p2 4个安装包之3
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 ...
httpwatch9.3.35 pro.zip
openssh 9.3p1 适用于centos9 redhat9操作系统安全加固、漏洞修复 x86架构 二进制rpm包,可直接安装
ArcEngine9.3.part12.rar
滚动截屏录屏(FastStoneCapture_9.3_PortableSoft).7z
openssh-9.3p1.tar.gz