微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,错误应该消失.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐