Ruby on Rails/ActionView/Uploads

From Wikibooks, open books for an open world
< Ruby on Rails‎ | ActionView
Jump to: navigation, search

Uploads[edit]

The following paragraph should give you just a quick overview on the topic of uploading data with forms. For the more advanced purposes you might want to consider using widely known gems that handle the job. Take a look at the Resources page to find good starting points for your search. Or take a look at the official Rails guide to see a sample action

As with everything you want to upload via a form, you need to set the form to support "multipart/form-data". If you want to upload, e.g a picture for our products, you can use these ways: Here, you will need to write an action inside the controller that handles the upload of the file on the server:

<% form_tag({:action => :upload}, :multipart => true) do %>
  <%= file_field_tag 'picture' %>
<% end %>

For a form bound to a model we can use the already known form_for tag (this allows you to save e.g. the image name inside your database)

<% form_for @person, :html => {:multipart => true} do |f| %>
  <%= f.file_field :picture %>
<% end %>