Chapter 8.3
this part, we will deal with the case that sign up success.
1. start from TDD!!!!
describe "success" do before(:each) do @attr = { :name => "New User", :email => "user@example.com", :password => "foobar", :password_confirmation => "foobar" } end it "should create a user" do lambda do post :create, :user => @attr end.should change(User, :count).by(1) end it "should redirect to the user show page" do post :create, :user => @attr response.should redirect_to(user_path(assigns(:user))) end end
there is nothing new:
a. lambda
b. should change(User, :count).by(1)
c. reponse.should redirect_to(user_path(assigns(:user)))
assigns is a hash that will be generated on every request, to have all instance variable.
2. now it is time the finish the sign up form.
if @user.save redirect_to @user else render :new end
note, we omit user_path in the redirect, this is also a rails convention.
3. the flash:
the content in flash is a hash, it will only exist for the next request, then disappear, so temporary.
you can iterate the flash content in this way:
flash.each do |key, value|
puts "#{key}"
puts "#{value}"
end
we will display the flash content in the application layout.
<% flash.each do |key, value| %> <div class="flash <%= key %>"><%= value %></div> <% end %>
you can fine this html code is very very ugly, to make it better, we can use the rails helper method
content_tag
<%= content_tag :div, value, :class => "flash #{key}" %>
for example, if flash[:success] = "Welcome to the sample app!"
the html will be:
<div class="flash success">Welcome to the Sample App!</div>
let's write a test to make sure the key :success appear.
it "should have a welcome message" do post :create, :user => @attr flash[:success].should =~ /welcome to the sample app/i end
note, we used
flash[:success].should =~ /welcome to the sample app/i
=~ is to see if a regex exist, and the last i indicate case insensitive.
for the =~
"jflkdsjlfsdj" =~ /abcd/
if not match, it will return nil
if match, it will return the index of the place starting match. (the index start from 0)
the make the test pass, we need to add this line into the controller code's create action:
if @user.save flash[:success] = "Welcome to the Sample App!" redirect_to @user
发表评论
-
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 7471. 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 6971. 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 733in 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 656again, 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 731whew!!!, 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 ... -
9.1 sessions
2011-10-12 10:00 643a 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 714in integration test, you can te ...
相关推荐
通过研究ZZCMS 8.3的源码,我们可以深入了解网站开发的内在逻辑,提升我们的编程技能和安全意识。 一、代码审计 1. 安全性检查:代码审计是确保软件安全的重要步骤,ZZCMS 8.3 的源码提供了实践机会,可以学习如何...
《Kuka Officelite8.3:高效办公的得力助手》 Kuka Officelite8.3是一款专为提高工作效率而设计的办公软件套装,它集成了多种基础且实用的办公工具,满足用户在日常工作中对文档处理、数据管理、演示文稿制作等多...
标题中的“Vpro8.3下载地址.rar”指的是Cognex的VisionPro 8.3版本的软件下载链接或资源的压缩文件。Cognex VisionPro是一款强大的机器视觉软件,广泛应用于工业自动化、质量控制、产品检测等领域。它提供了一系列...
kettle8.3 安装工具,内部网盘链接。kettle8.3 安装工具,内部网盘链接。kettle8.3 安装工具,内部网盘链接。
cognos 8.3 下载种子cognos 8.3 下载种子cognos 8.3 下载种子cognos 8.3 下载种子cognos 8.3 下载种子cognos 8.3 下载种子cognos 8.3 下载种子cognos 8.3 下载种子
carman_neutral-release.V8.3_202209091803.c7dbdc863f_83_9_AE_sign_8220.apk
SCRT8.3官方原版+SFX8.3官方原版+注册机,已测试可用。
升级 OpenSSH 到 8.3 OpenSSH 是一个开源的 SSH 服务器实现,广泛应用于 Linux 和 Unix 系统中。在本文中,我们将介绍如何升级 OpenSSH 到 8.3 版本。 为什么升级 OpenSSH OpenSSH 是一个基于 SSH协议的安全远程...
标题“protues8.3 sp1 licence”涉及的是Proteus软件的一个特定版本——Proteus 8.3 Service Pack 1(SP1)的许可证信息。Proteus是一款广泛应用于电子设计自动化(EDA)领域的仿真软件,由英国Labcenter ...
PostgreSQL 8.3 是一个历史悠久的版本,它在2007年发布,标志着PostgreSQL数据库管理系统的一个重要里程碑。PostgreSQL,简称Postgres,是一个开源的对象关系型数据库系统,以其强大、稳定和高度可扩展性而受到全球...
veritas netbackup 8.3 安装包
6.以上操作完成后,我们运行软件,这是您第一次运行此版本的SecureCRT 8.3,需要设置一个可以存储配置数据的文件夹。请在下面指定一个文件夹。按取消将不设置配置而退出应用程序夹。选择后点击ok。再点击Enter ...
**Ghost8.3详解** Ghost,全称是GHOST(General Hardware Oriented Software Transfer),是由赛门铁克公司(Symantec)开发的一款强大的磁盘映像备份与恢复工具。Ghost8.3作为Ghost系列的一个经典版本,因其高效、...
SecureCRT 8.3破解版,包含注册机,包含汉化补丁等,保证能用
8051 8.3 IAR IAR for 8051 8.30.2.part1
Mysql8.3数据库下载及安装教程
revolution8.3
最后,"SecureCRT 8.3.lnk"应该是启动SecureCRT 8.3版本的快捷方式。 综上所述,SecureCRT是一款全面而强大的终端工具,无论是在日常的系统维护,还是在复杂的企业级网络环境中,都能发挥出其独特的优势,提供安全...
工具SecureCRT 8.3则是一款非常优秀的终端仿真程序,通常利用软件把个人计算机仿真成一个终端与主机相连。现在几乎所有使用Linux的人都使用终端仿真方法。对于不使用X window的情况下,人们使用字符界面的终端(虚拟...