8.3 sign up success
3401 단어 sign
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 => "[email protected]",
: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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
8.3 sign up successthis part, we will deal with the case that sign up success. 1. start from TDD!!!! b. should change(User, :count).by(1) c...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.