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

时间:2014-07-28 19:14:05

标签: ruby-on-rails ruby simple-form

我有以下表格,我也在做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")

创建后,它当前转发到admin_collection_page_images_url或/ collection_page_images

我想在创建每条记录后简单地刷新并保持当前页面,是否有快速完成此操作的方法?

提前谢谢你。

1 个答案:

答案 0 :(得分:3)

您的控制器可能有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

如果记录中redirect_to成功,请注意.save指令。