简体   繁体   English

Rails AJAX重新加载页面。 为什么?

[英]Rails AJAX reload the page. Why?

I create a form: 我创建一个表单:

<%= form_for @cover, :remote => true, :html => { :multipart => true } do |file| %>
    <%= file.file_field(:image, :class => 'choose_image', :onchange => 'this.form.submit()') %>
    <input name="authenticity_token" type="hidden" value="<%=form_authenticity_token %>"/>
<% end %>

So, when I select image it uploads, OK, but after it reload the page and clear all my data. 因此,当我选择图像时,它会上传,确定,但是在重新加载页面并清除我的所有数据之后。 How can I repair it? 我该如何修理?

UPDATE 更新

My coversController: 我的coverController:

def create
    @cover = Cover.create(cover_params)

    if @cover.save
        redirect_to :back
    end
end

As a browser security aspect you can not upload image/file directly through jquery ajax. 作为浏览器的安全性方面,您不能直接通过jquery ajax上传图像/文件。 You either need to create an Iframe Or you can do it with flash(SWF). 您要么需要创建一个Iframe,要么可以使用Flash(SWF)来完成。

In your case :remote => true doing nothing it simply submitting form instead of ajax submit. 在您的情况下:remote => true执行任何操作,它只是提交表单而不是ajax提交。

You need to add remotipart gem to achieve your desired behaviour. 您需要添加remotipart宝石才能实现所需的行为。 In this gem also you need to add :remote => true and form will submit in ajax way. 在这个gem中,您还需要添加:remote => true ,表单将以ajax的方式提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM