简体   繁体   中英

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.

= 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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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