Using the form_for Method with multipart in Rails

If you’re going to upload files via an HTML form, the form’s encoding must be set to multipart/form-data. In Rails, you use a helper method to generate the form tags, so you can’t directly edit them. The preferred helper is form_for; that’s what scaffold generates.

Normally, a form_for call would look like this:

<% form_for(@entry) do |f| %>
  <!-- Form fields -->
<% end %>

To make this form be multipart, change the call to the following:

<% form_for(@entry, :html => {:multipart => true}) do |f| %>
  <!-- Form fields -->
<% end %>

5 thoughts on “Using the form_for Method with multipart in Rails

  1. There are a issue with Rails 2.3:

    uninitialized constant FileColumn::ClassMethods::Inflector

    ….
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:440:in `load_missing_constant’
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:80:in `const_missing’
    C:/ror/gia/vendor/plugins/file_column/lib/file_column.rb:619:in `file_column’

    Any idea for fix it?
    thanks

  2. You have an outdated version of FileColumn – theres someone else maintaining a version tahts rails 2.3 compatible. just google filecolumn rails 2

Leave a Reply

Your email address will not be published. Required fields are marked *