27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

First steps<br />

class ProjectsController < ApplicationController<br />

end<br />

Listing 3.13 app/controllers/projects_controller.rb<br />

def index<br />

end<br />

def new<br />

end<br />

Running rake cucumber:ok now results in a complaint about a missing new template,<br />

just as it did with the index action:<br />

When I follow "New Project"<br />

Missing template projects/new with {<br />

:handlers=>[:erb, :builder, :sass, :scss],<br />

:formats=>[:html],<br />

:locale=>[:en, :en]<br />

} in view paths "/home/rails3/ticketee/app/views"<br />

You can create the file at app/views/projects/new.html.erb to make this step pass,<br />

although this is a temporary solution. You come back to this file later to add content<br />

to it. The third step should now be the failing step, given the second one is passing, so<br />

run rake cucumber:ok to see if this is really the case:<br />

And I fill in "Name" with "TextMate 2"<br />

cannot fill in, no text field, text area or password field with<br />

id, name, or label 'Name' found (Capybara::ElementNotFound)<br />

Now Capybara is complaining about a missing "Name" field on the page it’s currently<br />

on, the new page. You must add this field so that Capybara can fill it in. Before you do<br />

that, however, fill out the new action in the ProjectsController so you have an object<br />

to base the fields on. Change the new to this:<br />

def new<br />

@project = Project.new<br />

end<br />

The Project constant is going to be a class, located at app/models/project.rb, thereby<br />

making it a model. A model is used to retrieve information from the database. Because<br />

this model inherits from Active Record, you don’t have to set up anything extra. Run<br />

the following command to generate your first model:<br />

rails g model project name:string<br />

This syntax is similar to the controller generator’s syntax except that you specified you<br />

want a model, not a controller. The other difference is that you gave it one further<br />

argument comprising a field name and a field type separated by a colon. When the<br />

generator runs, it generates not only the model file but also a migration containing the<br />

code to create this table and the specified field. You can specify as many fields as you<br />

like after the model’s name.<br />

63

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!