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.

260 CHAPTER 10 Tracking state<br />

Listing 10.15 db/seeds.rb<br />

State.create(:name => "New",<br />

:background => "#85FF00",<br />

:color => "white")<br />

State.create(:name => "Open",<br />

:background => "#00CFFD",<br />

:color => "white")<br />

State.create(:name => "Closed",<br />

:background => "black",<br />

:color => "white")<br />

If you try to run rake db:seed now, you see that this task was aborted:<br />

rake aborted!<br />

Validation failed: Email has already been taken<br />

(See full trace by running task with --trace)<br />

When a rake task aborts, it means an exception has been raised. As the output suggests,<br />

you can see the backtrace by running the same command with --trace :rake<br />

db:seed --trace. You’ll now be given the complete backtrace of your rake task and<br />

can determine what broke. The first line of application-related backtrace in the output<br />

provides a useful clue:<br />

/home/you/ticketee/db/seeds.rb:1:in `'<br />

It’s the first line of db/seeds.rb that’s causing the problem! This is the line that creates<br />

your admin user, and it’s rightly failing because you already have a user with the email<br />

address admin@ticketee.com. Let’s comment out these first couple of lines as well as<br />

the line that creates the Ticketee Beta project, because you don’t want two Ticketee<br />

Beta projects. The only line left uncommented in your seeds file should be the line<br />

you’ve just added. When you run rake db:seed again, it will run successfully. Let’s<br />

uncomment these lines that you’ve just commented out.<br />

With these states now defined inside db/seeds.rb, your feature at features/seed<br />

.feature will pass:<br />

1 scenario (1 passed)<br />

10 steps (10 passed)<br />

Also, with your states seeding, you can go back to your<br />

server at http://localhost:3000 and create a comment on<br />

your ticket with any status, because you’re trying to figure<br />

out why the “Creating comments” feature is failing. After<br />

creating your comment, you should see that the ticket’s Figure 10.8 Ugly state output<br />

state doesn’t display as simple text like New, Open, or Closed, but rather as a standard<br />

inspect output, as shown in figure 10.8.<br />

Well, isn’t that ugly and not user-friendly? It flows off the end of the ticket box!<br />

Thankfully, you can fix this by defining the to_s method in your State model to call<br />

the name method:

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

Saved successfully!

Ooh no, something went wrong!