[英]Rails 3 AJAX page redirecting after uploading image
I am using AJAX to upload an image (with carrierwave gem) and want to display the image after uploading without refreshing or redirecting the page. 我正在使用AJAX上传图像(带有carrierwave gem),并希望在上传后显示图像而不刷新或重定向页面。 Currently, after I upload an image it redirects to /images rather than staying on the / home page. 目前,在我上传图片后,它重定向到/ images而不是停留在/主页上。
image_controller.rb: image_controller.rb:
def create
@image = Image.new(params[:image])
respond_to do |format|
if @image.save
format.js
end
end
end
_upload.html.erb: _upload.html.erb:
<%= form_for @image, :html => { :multipart => true }, :remote => true do |f|%>
<p>
<%= f.file_field :image %>
</p>
<p><%= f.submit "Upload image", :class => 'btn primary', :id => 'image-upload' %></p>
<% end %>
create.js.erb: create.js.erb:
$('images-show').replaceWith('<%=j render 'shared/show_image' %>');
How do I prevent the redirect to /images after an image upload? 上传图片后,如何防止重定向到/ images?
For starters, make sure you have gem 'jquery-rails'
in your Gemfile. 首先,请确保您的Gemfile中包含gem'jquery gem 'jquery-rails'
。 Then make sure that you have <%= javascript_include_tag "application" %>
in the head of your html document application.html.erb
and that the file you are including, application.js
has the lines: 然后确保您在html文档application.html.erb
的头中具有<%= javascript_include_tag "application" %>
,并且要包含的文件application.js
包含以下行:
//= require jquery
//= require jquery_ujs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.