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.

Attaching many files<br />

TicketsController’s new action, however, you don’t initialize any assets for this ticket,<br />

so assets returns an empty array and no fields at all are displayed.<br />

To get this action to render three file input fields, you must initialize three Asset<br />

objects associated to the Ticket object the form uses. Change your new action inside<br />

TicketsController to this:<br />

def new<br />

@ticket = @project.tickets.build<br />

3.times { @ticket.assets.build }<br />

end<br />

The final line of this action calls @ticket.assets.build three times, which creates<br />

the three Asset objects you need for your fields_for.<br />

When you run the scenario again, the three fields are available, but the scenario<br />

now fails because it can’t find a file to upload:<br />

And I attach the file "spec/fixtures/speed.txt" to "File #1"<br />

And I attach the file "spec/fixtures/spin.txt" to "File #2"<br />

And I attach the file "spec/fixtures/gradient.txt" to "File #3"<br />

And I press "Create Ticket"<br />

/home/you/ticketee/spec/fixtures/<br />

spin.txt file does not exist (RuntimeError)<br />

Create this spin.txt file now inside the spec/fixtures directory, and give it the following<br />

content:<br />

Spinning blink tags have a 200% higher click rate!<br />

You also need to add the gradient.txt file to the same directory, and it contains this:<br />

Everything looks better with a gradient!<br />

These two text pieces are random filler meant only to provide some text if you ever<br />

need to reference it. Let’s run the scenario again:<br />

And I press "Create Ticket"<br />

unknown attribute: asset (ActiveRecord::UnknownAttributeError)<br />

You got this message earlier when you were implementing single-file uploading<br />

because you didn’t define has_attached_file on the Ticket class. Let’s get more<br />

backtrace for this error by using the -b switch at the end of the command: bin/cucumber<br />

features/creating_tickets.feature:36 -b. This command provides the whole<br />

backtrace for an error. In this particular backtrace, you’re after anything to do with<br />

assets, because that’s the only thing that’s changed since this feature was passing. You<br />

should see a line like the following about 10 lines down:<br />

.../active_record/nested_attributes.rb:254:in 'assets_attributes='<br />

This line indicates that the failure most likely has to do with the assets_attributes=<br />

method, which was kindly enough provided by Rails through the call to<br />

accepts_nested_attributes_for. If this error occurs after the assets_attributes=<br />

method, then it definitely has to do with this method. In fact, it’s probably because<br />

225

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

Saved successfully!

Ooh no, something went wrong!