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.

你可能感兴趣的:(controller)