首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

5.3 create a controller for users to register our site

2012-10-06 
5.3 create a controller for users to register our site.1.?rails generate controller Users new?this

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.

热点排行