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.

Using JavaScript<br />

The element this updates doesn’t currently exist, but you can easily create it by<br />

wrapping the fields_for inside a div with the id attribute set to files, as shown in<br />

the following listing.<br />

Listing 9.14 app/views/tickets/_form.html.erb<br />

<br />

number do |asset| %><br />

<br />

<br />

<br />

<br />

<br />

<br />

This div tag provides an element for your new link_to to insert a file field into. If you<br />

run this scenario with bin/cucumber features/creating_tickets.feature:36, the<br />

step that follows the Add Another File link passes, but the file field is still not visible:<br />

And I follow "Add another file"<br />

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

cannot attach file, no file field with id, name, or label 'File #2'<br />

The Add Another File link currently uses the new_file_path helper, which generates<br />

a route such as /files/new. This route points to the new action in FilesController.<br />

This action isn’t defined at the moment, so the feature won’t work. Therefore, the<br />

next step is to define the action you need.<br />

9.4.4 Responding to an asynchronous request<br />

The job of the new action inside the FilesController is to render a single file field<br />

for the ticket form so users may upload another file. This action needs to render the<br />

fields for an asset, which you already do inside app/views/tickets/_form.html.erb by<br />

using these lines:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

To re-use this code for the new action in FilesController, move it into a partial<br />

located at app/views/files/_form.html.erb.<br />

In app/views/tickets/_form.html.erb, you can replace the lines with this simple<br />

line:<br />

"files/form",<br />

:locals => { :number => number } %><br />

235

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

Saved successfully!

Ooh no, something went wrong!