Multipart form not working in Rails

╄→尐↘猪︶ㄣ 提交于 2019-12-11 11:11:54

问题


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.


回答1:


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



回答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)




回答3:


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.



来源:https://stackoverflow.com/questions/8875603/multipart-form-not-working-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!