Creating a Controller by Hand

It may be tempting at this point to run a Rails generator to generate the controller, views, helpers, tests, and who knows what other files. We’ll resist this temptation and implement the bare minimum by hand. This way, we don’t end up with a lot of code that isn’t tested and that we might not even need.

Let’s map our controller to config/routes.rb:

rails/10/config/routes.rb
 
Squeaker::Application.routes.draw ​do
 
resources :users
 
end

Add app/controllers/users_controller.rb:

rails/10/app/controllers/users_controller.rb
 
class​ UsersController < ApplicationController
 
def​ show
 
end
 
end

And a view in app/views/users/show.html.erb with some temporary content:

rails/10/app/views/users/show.html.erb
 
Hello

Admittedly, we did take a couple of shortcuts here. A fundamentalist BDD practitioner would have run Cucumber between each little code addition, getting guidance at every little step on the way. We recommend doing this in the beginning until you find your own comfort level regarding how much code is safe to add, remove, or modify between each run. This is a trade-off between development speed and confidence.

Many people set up their environment so that Cucumber runs in the background every time you save a file. Explaining how do this is beyond the scope of this book, but we recommend you check out Guard[61] or Watchr.[62] They are both easy to set up and fun to use!

With the route, controller, and view in place, let’s run Cucumber once more to see where we’re at:

 
Feature: See Messages
 
 
Scenario: See another user's messages
 
Given there is a User
 
And the User has posted the message "this is my message"
 
When I visit the page for the User
 
Then I should see "this is my message"
 
Undefined step: "I should see "this is my message"" (Cucumber::Undefined)
 
features/see_messages.feature:6
 
 
1 scenario (1 undefined)
 
4 steps (1 undefined, 3 passed)
 
0m0.423s
 
 
You can implement step definitions for undefined steps with these snippets:
 
 
Then /^I should see "(.*?)"$/ do |arg1|
 
pending # express the regexp above with the code you wish you had
 
end

Whoa! We successfully made a request to our user page. The last step is still undefined, so we need to implement that as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.51.145