在Rails ActiveAdmin中编辑后如何重定向到页面索引

问题描述 投票:0回答:1

我有一个带有运行ActiveAdmin的Rails 6站点的客户端,该客户端希望在选择编辑和更新记录后将用户返回到他或她在索引中所在的页面。

例如,如果用户在页面/admin/employees?page=2上并单击某个雇员的“编辑”链接,则在更新该雇员记录后,他们想返回到/admin/employees?page=2

我已经知道如何将其返回到索引页面而不是显示页面,我只是无法让它返回到它所在的页面。

任何人都可以帮忙吗?

ruby-on-rails activeadmin
1个回答
0
投票

假设您正在使用会话,然后遵循此处的建议:

Link back to page visited before form

应该有帮助。要将其应用于active_admin,请在app/admin/employees.rb中,每次单击编辑页面时首先保存最后一个URL,然后在成功更新后将其重定向到它:

controller do
  def edit
    session[:my_previous_url] = URI(request.referer || '').path
    super
  end

  def update
    @employee.update(permitted_params[:employee])
    # check if the previous URL was correctly saved in session, if so use that, if not use employees index
    redirect_target = session[:my_previous_url].blank? ? employees_path : session[:my_previous_url]
    if @employee.save
      redirect_to redirect_target
    else
      render :edit
    end
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.