ruby-on-rails – 在Rails 4中发送一个带有远程true的表单

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在Rails 4中发送一个带有远程true的表单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个用于更新图像的表单
<%= form_for current_user,url: update_image_user_path(current_user),method: :post,html: {multipart: :true,remote: true},:authenticity_token => 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格式发送的

我错过了什么?

解决方法

你应该知道几件事.

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

A. https://github.com/tors/jquery-fileupload-rails
B. http://railscasts.com/episodes/381-jquery-file-upload
> remote:true不是html属性.改变你的代码

<%= form_for current_user,html: {multipart: :true},remote: true,:authenticity_token => true do |f| %>

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

:formats=>[:html]

应该说:

:formats=>[:js]

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

原文链接:https://www.f2er.com/ruby/268671.html

猜你在找的Ruby相关文章