Multipart form not working in Rails

2019-07-28 14:43发布

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条回答
淡お忘
2楼-- · 2019-07-28 15:11

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)

查看更多
Root(大扎)
3楼-- · 2019-07-28 15:14

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 %>
查看更多
闹够了就滚
4楼-- · 2019-07-28 15:18

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.

查看更多
登录 后发表回答