欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

5.3 create a controller for users to register our site

更新时间: 2014-01-05 02:58:57 责任编辑: Author_N1

 

5.3 create a controller for users to register our site.

1. rails generate controller Users new

 

this line of code will generate a users controller and a new action, 

also a spec test file of 

 

users_controller_spec.rb

 

and also a route

 

get "users/new"

 

2. next, we will start another loop of test-driven development:

 

first, we add another test into users_controller_spec.rb

 

 

it "should have the right title" do
    get 'new'
    response.should have_selector('title', :content=>"Sign up")
end

 

note, we need add "render_views", to make sure the test will render views to test.

if not, the pass won't pass, even if we added the title.

 

note, we used have_selector again.

 

3. ok, cool, we got a failing test, next, let's make it pass.

 

a. define a instance var into new method.

 

@title = "Sign up"

 

ok, done!

 

4. we already got a route:

get "users/new"

but we don't want to use it, we want to use

"/signup"

 

so let's define another route:

 

match "/signup" => "users#new"

 

But, wait, before we add this line of code to route, let's write failing test first:

 

since we are going the test route of "/signup", which doesn't belong to any controller.

 

so this test need to go to integration test.

 

 

it "should have a sign up page at '/signup'" do
    get "/signup"
    response.should have_selector "title", :content => "Sign up"
end

 ok, this test will fail, then we can add that route to make it pass!!!

 

5. remember? that route will also create a helper:

 

signup_path

 

we can use this path in views, instead of hardcode paths.

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:58:57
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/16956/
本文WWW.DOC100.NET DOC100.NET版权所有。