Antun’s Blog

Notes on OpenLaszlo, LZX and Rich Internet Application Development

Antun’s Blog header image 2

Using the form_for Method with multipart in Rails

August 22nd, 2008 · 4 Comments

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 %>

Tags: Rails

4 responses so far ↓

  • 1 Fer // Jun 2, 2009 at 5:24 pm

    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 Stephen // Jul 30, 2009 at 7:06 am

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

  • 3 Ravindra Singh // Jan 7, 2010 at 5:06 am

    use ActiveSupport::Inflector on line
    file_column.rb:619

  • 4 Rafael // Oct 9, 2011 at 10:28 am

    TKS! This helped me a lot!

Leave a Comment

Need to post code in a comment? Read this first!