[英]Rails 3 AJAX page redirecting after uploading image
我正在使用AJAX上傳圖像(帶有carrierwave gem),並希望在上傳后顯示圖像而不刷新或重定向頁面。 目前,在我上傳圖片后,它重定向到/ images而不是停留在/主頁上。
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:
<%= 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:
$('images-show').replaceWith('<%=j render 'shared/show_image' %>');
上傳圖片后,如何防止重定向到/ images?
首先,請確保您的Gemfile中包含gem'jquery gem 'jquery-rails'
。 然后確保您在html文檔application.html.erb
的頭中具有<%= javascript_include_tag "application" %>
,並且要包含的文件application.js
包含以下行:
//= require jquery
//= require jquery_ujs
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.