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

做~自己de王妃 提交于 2020-01-21 12:22:09

问题


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 %>");

回答1:


Try to use remotipart gem https://github.com/JangoSteve/remotipart




回答2:


Following Solution work for me

$('.sales_application_form').submit(function() {
    $(this).ajaxSubmit();
    return false;
  });


来源:https://stackoverflow.com/questions/11008764/rails-form-for-with-file-field-and-remote-true-and-format-js

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