在Rails 4中发送带有远程true的表单

我有一个用于更新图像的表单

 true do |f| %> 

行动有

 respond_to do |format| format.js format.html end 

但我收到以下错误

 ActionView::MissingTemplate - Missing template users/update_image, application/update_image with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}. 

我没有update_image.html.erb模板,但是从错误中我得知请求不是以js格式发送的

我错过了什么?

你应该知道几件事。

  1. Ajax使用称为xmlhttprequest的东西来发送数据。 不幸的是,xmlhttprequests无法发布文件。 Remotipart可能会工作,但在jqueryfileupload上有更多的文档和轨道投射video。 它被许多站点使用(很多站点甚至不使用rails)。 我建议使用jqueryfileupload-rails gem和rails cast:

    A. https://github.com/tors/jquery-fileupload-rails
    B. http://railscasts.com/episodes/381-jquery-file-upload

  2. remote:true不是html属性。 改变你的代码:

     <%= form_for current_user, url: update_image_user_path(current_user), method: :post, html: {multipart: :true}, remote: true, :authenticity_token => true do |f| %> 

您遇到的错误中最重要的部分是:

 :formats=>[:html] 

应该说:

 :formats=>[:js] 

使用remote:现在在正确的位置为true,错误应该消失。

rails remote true不适用于图像上传。 您不能仅使用远程true来使用ajax上传图像。 你需要jquery.form.js文件给用户ajaxForm。 对于这包括

   

并绑定表格

  $("#formid").ajaxForm() 

或者,您可以使用jqueryfileupload-rails gem

https://github.com/tors/jquery-fileupload-rails

怎么样的html: {multipart: :true}, remote: true而不是html: {multipart: :true, remote: true}