1

I have been trying to create a multi-part form in Rails for the last day, which is crazy, but I am really not sure how to get around this one.

Currently, here is the code in my view:

<%= form_for @account, :html => {:multipart => true } do |f| %> 

However, the HTML returned is as follows:

<form accept-charset="UTF-8" action="/accounts/1" class="edit_account" id="edit_account_1" method="post">

For the life of me I can't figure out why the form is not showing up as a multi-part. This particular form is used to upload an image using paperclip to AWS, but fails each time, presumably because it isn't a multipart form.

Help! :) And thanks.

3 Answers3

4

Hi according to Rails API v3.1.3, your code should look like following:

 <%= form_for @account,{},:html => {:multipart => true } do |f| %> 

The difference is by passing empty options to rails helper and it will read your html parameters.

Thanks

UPDATE:

Here is the code copied from one of my projects: It is working and runs under Rails 3.1

May be you could try to put brackets after the "form_for"

<%= form_for(@account,{},:html => { :id=>"account_form",:multipart => true }) do |f| %>
  <%= render :partial => "form", :object => f %>
  <%= f.submit 'create' %>
<% end %>
peterpengnz
  • 5,897
  • 2
  • 21
  • 18
  • Unfortunately this yields the same result - it does not add the enctype parameter to the HTML output. Thanks very much for trying though! – Steven Michael Thomas Jan 19 '12 at 06:13
  • @StevenMichaelThomas I have updated the answer with the real working code copied from my project. Hopefully, it would give you some idea. Cheers – peterpengnz Jan 19 '12 at 08:49
2

This worked for me.

<%= form_for(@account, html: { :multipart => true }) do |f| %>

or

<%= form_for(@account, html: { :enctype => 'multipart/form-data' }) do |f| %>

As per @peterpengnz's answer, providing an empty {} parameter to form_for got ArgumentError:

wrong number of arguments (3 for 1..2)

Sithu
  • 4,752
  • 9
  • 64
  • 110
1

It turns out that I'm a huge idiot, and the original form was working fine, EXCEPT...

I was rendering the form in a partial, but I wrapped the partial in a standard, non-multipart form tag, which overwrote the multipart form, and somewhat surprisingly didn't raise any errors.

Either way, I am a stupid one for not noticing this, but it is now resolved and the file uploading is working perfectly.