27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

124 CHAPTER 6 Authentication and basic authorization<br />

In this step definition, you use Cucumber’s table format again to specify more than<br />

one user to create. To get to these attributes, iterate through table.hashes, storing<br />

each set of attributes as attributes for each iteration. Inside the iteration, the<br />

create! method creates the user record using these attributes. All of this should look<br />

pretty familiar—you used it to create tickets for a project.<br />

The second step in this scenario is provided by the email_spec gem, and it fails<br />

when you run bin/cucumber features/signing_in.feature again:<br />

And "user@ticketee.com" opens the email with subject "Confirmation<br />

➥instructions"<br />

Could not find email With subject "Confirmation instructions".<br />

Found the following emails:<br />

[]<br />

This is email_spec telling you it can’t find an email with the title “Confirmation<br />

instructions,” which is what Devise would send out if you had told it you wanted users<br />

to be confirmable. You haven’t yet done this, so no emails are being sent.<br />

To make users confirmable, add the :confirmable symbol at the end of the<br />

devise call in app/models/user.rb:<br />

devise :database_authenticatable, :registerable,<br />

:recoverable, :rememberable, :trackable,<br />

:validatable, :confirmable<br />

Now Devise will send confirmation emails when users sign up. When you run bin/<br />

cucumber features/signing_in.feature again, your first step is failing:<br />

Given there are the following users:<br />

| email | password | unconfirmed |<br />

| user@ticketee.com | password | true |<br />

undefined local variable or method 'confirmed_at' for #<br />

The confirmed_at attribute is used by Devise to determine whether or not a user has<br />

confirmed their account. By default, this attribute is nil, indicating the user hasn’t<br />

confirmed yet. The attribute doesn’t exist at the moment, so you get this error.<br />

You could add this attribute to the existing db/migrate/[timestamp]<br />

_devise_create_users.rb migration, but because you already pushed that migration, you<br />

should avoid changing it, as others will have to rerun the migration to get those<br />

changes. Even though it’s just you working on the project at the moment, it’s a good<br />

rule of thumb to not modify migrations that have already been pushed.<br />

Instead, create a new migration to add the confirmed_at field and two others,<br />

confirmation_token and confirmation_sent_at. The confirmation_token is generated<br />

by Devise and used to identify users attempting to confirm their account when<br />

they click the confirmation link from the email. The confirmation_sent_at field is<br />

used also by Devise and tracks the time when the confirmation email was sent:<br />

rails g migration add_confirmable_fields_to_users<br />

Let’s now open this migration and put the code from the following listing inside it.

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

Saved successfully!

Ooh no, something went wrong!