Multipart form not working in Rails - ruby-on-rails

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.

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

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)

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.

Related

Is it correct to specify ":html => { :multipart => true}" for form_for?

We're running Rails 3.2.19 and Ruby 2.1.2.
We have a legacy line of code (actually, more than one, but this line is of particular concern) that reads
form_for [#commentable, #comment], :html => { :multipart => true, :class => "lightbox_form"} do |f|
The form data may or may not include an uploaded file at the user's discretion.
Many examples in SO refer to specifying the :multipart setting, but then I see this answer at Form_for with :multipart => true spits out and then in looking at the docs at http://guides.rubyonrails.org/v3.2.19/form_helpers.html#uploading-files, I see that it shouldn't be necessary (except if using form_tag). I also see this discussion at https://github.com/rails/rails/issues/10176 that adds to my confusion.
I'm asking in part because we're getting EOF errors in Rack (no content coming through on the multipart parser; see Rack throwing EOFError (bad content body) if you're interested in those details).
Our code may have been previously run under earlier versions of Rails when it may have been necessary (and so may just be a holdover). But given all the other examples on SO that include :multipart, I want to better understand if or when :multipart is needed with form_for before I remove it and what side effects I may encounter.
It's not required in Rails 3.1 and later, as long as you're using form_for with file_field like this:
<%= form_for #person do |f| %>
<%= f.file_field :picture %>
<% end %>
This will not work:
<%= form_for #person do |f| %>
<%= file_field_tag 'person[picture]' %>
<% end %>
You can easily verify that it's working. Inspect the generated HTML and look for the enctype="multipart/form-data" attribute on the form tag. Rails doesn't do any magic beyond setting the encoding type, so if the attribute is there, you're good.

undefined method `model_name' for NilClass:Class on view

My code is working on local server but it's not working on the production server. I can't figure it out what I'm doing wrong.Please help me.
This is where I'm getting error in my partial:
<%=form_for #shiftchange, :url => { :controller=>"schedulers" ,:action => "shift_change" },:validate=>true , :method => :post do |f|%>
<%= f.label :from_date , "From Date " %>
<%= f.text_field :from_date ,:class =>'datepicker' %>
<% end %>
To load the partial,this is what I'm doing this:-
<%= render "schedule_shift" %>
In the controller I have this:
#shiftchange = Shiftchange.new
If the form is included for multiple actions (pages) you need to set #shiftchange to something or the form_for directive will fail (it won't have anything to create a form for).
A much better approach (and the Rails default) is to have separate views for each action, each including only the elements required by that action, and loaded into your application.html.erb layout with <%= yield %>. So you would have a app/views/shiftchanges/new.html.erb view which has the form in it. You never very rarely need to define any load paths in Rails, they are all derived from the model, controller and action names - or from your routes.rb. This is a core part of the convention over configurationhttp://en.wikipedia.org/wiki/Convention_over_configuration paradigm which runs so deep in Rails.
If you do need to have a form for creating a new object on every page (often used for ShiftchangeSessions for example), you can rewrite your form_for so that it doesn't depend on an object being present:
form_for(Shiftchange.new)
or if you need to force it to post to the create method
form_for(Shiftchange.new, :url => { :action => "create" })
You can read more about resource driven form_for in the Ruby On Rails API docs http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for-label-Resource-oriented+style.
Change
form_for #shiftchange
for this
form_for :shiftchange
i guess it works

Rails form_for with file_field and remote => true and format => :js

I am submitting a form_for with a file_field in it. When the form is submitted with a post, the controller responds with a .js file which will be executed.
The file_field is optional.
When I upload a file, the js file is opened in a new document (as a simple text file) and the js is not executed. When I don't upload any file, the response div is updated. I want the update success to be notified in the response div or a failure message independent of the file upload.
May be this is done for some security reasons? Is there some workaround for people who want to be able to do this?
Roughly,
Form partial _action.html.erb:
<div id="response"></div>
<%= form_for ... :remote => true, :url => {..., :format => :js} do |f| %>
...
<%= f.file_field :name %>
<% end %>
Controller::action
if not request.post?
render :partial => 'controller/action'
return
else
#response = "save was successful";
# renders the action.js.erb
end
action.js.erb:
$("#response").html("<%= #response %>");
Try to use remotipart gem https://github.com/JangoSteve/remotipart
Following Solution work for me
$('.sales_application_form').submit(function() {
$(this).ajaxSubmit();
return false;
});

rails form does not work anymore

This form to update a work item does not work anymore.
The form is generated in the erb file using a this command:
<%= form_for(#work_item, :url => admin_workitem_update_path) do |f| %>
The generated tag looks like this:
<form accept-charset="UTF-8" action="/admin/workitem/define/14" class="edit_workitem" enctype="multipart/form-data" id="edit_workitem_14" method="put">
</form>
The route looks like this:
admin_workitem_update PUT /admin/workitem/define/:workitem_id(.:format)
Follow-up: using :method => :post in the form tag works.
Why does rails generate the put method attribute?
NEVER use equal sign befor form. I have the same problem with Instant Rails implementation. The tutorial on rails documentation should be used without <&=
<%= form_for(#work_item, :url => admin_workitem_update_path) do |f| %>
use insteand:
<% form_for(#work_item, :url => admin_workitem_update_path) do |f| %>
this is all

Rails file_field is not returning original_filename

I have the below form in my view:
<% form_for(#filedata, :url=>{ :controller=>"filedatas", :action=>"create"}, :multipart => true) do |f| %>
<label>Select File</label> : <%#= f.file_field :file %>
<%= file_field_tag 'uploadedFile' %>
<%= f.submit 'Upload' %>
</p>
<% end %>
I've commented out the f.file_field but I have tested on both and both give me the same problem. They just return the name of the file and I get a string. methods like .path and .original_filename cause an error.
In my list of parameters I get:
"uploadedFile"=>"test"
(the name of my file is test.txt)
Anyone have any suggestions?
Sorry, I may have misunderstood your original question. It looks like you have an error in your form_for call. It should be:
<% form_for(#filedata, :url=>{ :controller=>"filedatas", :action=>"create"}, :html => {:multipart => true}) do |f| %>
You were missing the ":html =>{}" part. Also, you can shorten it down like this:
<%= form_for #filedata, :html => {:multipart => true} do |f| %>
This is happening because the contents of the file aren't stored in the attribute like other fields. File contents are stored in the POST data, and have to be retrieved. Extra steps are required.
This article explains one way to do it manually. But a better approach is to use a plugin like thoughtbot's paperclip to handle this for you.

Resources