【问题标题】:HowTo Redirect To Index with Page after edit in Rails ActiveAdmin在 Rails ActiveAdmin 中编辑后如何使用页面重定向到索引
【发布时间】:2020-07-05 12:12:52
【问题描述】:

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

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

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

谁能帮忙?

【问题讨论】:

    标签: ruby-on-rails activeadmin


    【解决方案1】:

    假设您正在使用会话,那么请遵循此处的建议:

    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
    

    【讨论】:

    • 不知道为什么,但 my_previous_url 没有保存到会话中
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多