简体   繁体   English

Rails创建新记录而不重定向而是刷新并保留在当前页面上

[英]Rails create new record without redirect instead refresh and stay on current page

I have the following form, also I'm doing CollectionPageImage.new because its creating new record from another model. 我有以下形式,我也在做CollectionPageImage.new因为它从另一个模型创建新记录。

= simple_form_for(Spree::CollectionPageImage.new, :url => admin_collection_page_images_url, :html => { :multipart => true, }) do |f|
  = render :partial => 'spree/shared/error_messages', :locals => { :target => @collecion_page_images }
  = f.input :image, :label => "Collection images: "
  = f.association :collection_page
  = @collection_page.title
  = submit_tag t("create")

After it is created it is currently forwarding to admin_collection_page_images_url or /collection_page_images 创建后,它当前转发到admin_collection_page_images_url或/ collection_page_images

I would like it to simply refresh and stay on the current page after each record is created, is there a quick way accomplish this? 我想在创建每条记录后简单地刷新并保持当前页面,是否有快速完成此操作的方法?

Thank you in advance. 先感谢您。

You probably have a create action of your controller that looks kind of like this: 您可能有一个控制器的create操作,看起来像这样:

def create
  @collection_page_image = CollectionPageImage.new(collection_page_image_params)

  if @collection_page_image.save
    # Here, redirect to the edit page of the just-created record
    redirect_to admin_collection_page_image_url(@collection_page_image)
  else
    flash[:errors] = @collection_page_image.errors
    redirect_to action: :new
  end
end

Notice the redirect_to instruction if the .save is successfull on the record. 如果.save在记录上成功,请注意redirect_to指令。

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

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