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.

218 CHAPTER 9 File uploading<br />

remove_column :tickets, :asset_file_size<br />

remove_column :tickets, :asset_updated_at<br />

end<br />

end<br />

Remember that you call the field asset internally, but to the user, it’s called File? This<br />

column-naming convention is the reason for the different names. To ease confusion<br />

for people working on the application (you!), you call these fields with the asset prefix<br />

so that the column names are asset_file_name and not file_file_name. There’s<br />

also another reason, which is explained in section 9.2.<br />

To add these columns to your development environment’s database, run rake db<br />

:migrate. Then run rake db:test:prepare to add them to your test environment’s<br />

database. If you run the feature with bin/cucumber features/creating_tickets<br />

.feature, all the scenarios that were previously passing are still passing. But the scenario<br />

you just added fails with this error:<br />

Then I should see "speed.txt" within "#ticket .asset"<br />

You can see that the scenario failed because Capybara can’t find the text within this<br />

element on the TicketsController’s show page: this text and this element don’t<br />

exist! You need to add at least the filename for your scenario to pass, so add it underneath<br />

the spot in the show view where you currently have the following:<br />

Created by <br />

<br />

You must also wrap all the code in this view inside a div tag with the id attribute<br />

ticket and spice it up a little by adding the content type and file size there too, as<br />

shown in the following listing.<br />

Listing 9.5 app/views/tickets/show.html.erb<br />

Created by <br />

<br />

<br />

Attached File<br />

<br />

<br />

<br />

<br />

<br />

()<br />

<br />

<br />

Provide<br />

download link<br />

Here you use the exists? method defined on the asset method (which was defined<br />

by Paperclip and returns a Paperclip::Attachment object); the exists? method<br />

returns true if the file exists. You use it because you don’t want it trying to display the<br />

path or any other information of a file when there isn’t one.<br />

B

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

Saved successfully!

Ooh no, something went wrong!