Rails - 从 2 个视图重定向到 1 个 edit_view,然后使用 get-params 返回

Rails - Redirection from 2 views to 1 edit_view and back with get-params

我有 2 个视图,两个视图上都有一个编辑按钮。 此按钮重定向到 edit_view。 如果我在那里提交,重定向应该让我回到我来自的视图。并通过 URL 中的 get 将 id 参数传递回其中一个视图。

型号:

localhost:3000/order/list > /order/edit_single_order > localhost:3000/order/list?id=1

localhost:3000/order/administrate > /order/edit_single_order > /order/administrate

重定向:

    def redirect_to_back_or_default_params(default = root_url, *args)
      if request.env['HTTP_REFERER'].present? && request.env['HTTP_REFERER'] != request.env['REQUEST_URI']
        redirect_to :back, *args
      else
        redirect_to default, *args
      end
    end

控制器重定向:

redirect_to_back_or_default_params administrate_order_path(:provider_id => @cart_item.product.provider.id)

问题已解决: 我用 store_location

传递了参数

Controller/View :

store_location(params[:provider_id])

Store_location:

  def store_location(args=nil)
    #test to prevent Url's like order/administrate?id=       
    if args.nil?
      session[:return_to] = "#{request.protocol}#{request.host_with_port}#{request.fullpath}"     
    else 
      session[:return_to] = "#{request.protocol}#{request.host_with_port}#{request.fullpath}?id=#{args}"
    end
  end

重定向:

    def redirect_back_or_default(default)
      redirect_to(session[:return_to] || default)
      session[:return_to] = nil
    end