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 738the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8051. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 8771. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8361.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7021. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8581. 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 10631. 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 603again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6621. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 682whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 6811. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 670There’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 ...
相关推荐
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
Vpro8.3下载地址.rar
升级openssh到8.3
6.以上操作完成后,我们运行软件,这是您第一次运行此版本的SecureCRT 8.3,需要设置一个可以存储配置数据的文件夹。请在下面指定一个文件夹。按取消将不设置配置而退出应用程序夹。选择后点击ok。再点击Enter ...
8051 8.3 IAR IAR for 8051 8.30.2.part1
Veritas NetBackup 8.3
revolution8.3
idapro8.3
Cognos 8.3官方文档 Cognos 8.3官方文档
nero8.3.6.0
MyDAC 8.3 for XE6 vcl control
cuteftp8.3注册机下载,绝对可以破解。破解方法: 解压,打开cuteftp+8.3注册机,点应用补丁,然后“是”,找到cuteftp的安装位置,双击cuteftppro(.exe)这个应用文件,注册,提示done,表示OK,退出注册机之后再...
共5个(part1/2/3/4/5)分包,全部下载后用命令 “copy /b pdi-ce-8.3.0.0-371.zip.rm1+pdi-ce-8.3.0.0-371.zip.rm2+pdi-ce-8.3.0.0-371.zip.rm3+pdi-ce-8.3.0.0-371.zip.rm4+pdi-ce-8.3.0.0-371.zip.rm5 pdi-ce-8.3...
Dvbbs8.3_Sql版 Dvbbs8.3 动网论坛8.3 动网论坛8.3Sql版 动网论坛8.3下载
kettle8.3 安装工具,内部网盘链接。kettle8.3 安装工具,内部网盘链接。kettle8.3 安装工具,内部网盘链接。
pdi-ce-8.3(kettle 8.3)安装软件, 百度网盘地址(含密码 2019年10月最新版)
rhel 8.3 最新版安装iso下载,redhat公司最新企业版操作系统! 链接:https://pan.baidu.com/s/1EzBdknI6lDr-2W1YNXep6A 提取码:chd8 复制这段内容后打开百度网盘手机App,操作更方便哦
打字高手8.3的注册机