How do I do a redirection in routes.rb passing on the query string

心不动则不痛 提交于 2019-12-10 01:02:31

问题


I had a functioning redirect in my routes.rb like so;

match "/invoices" => redirect("/dashboard")

I now want to add a query string to this so that, e.g.,

/invoices?show=overdue

will be redirected to

/dashboard?show=overdue

I've tried several things. The closest I have got is;

match "/invoices?:string" => redirect("/dashboard?%{string}")

which gives me the correct output but with the original URL still displayed in the browser.

I'm sure I'm missing something pretty simple, but I can't see what.


回答1:


You can use request object in this case:

match "/invoices" => redirect{ |p, request| "/dashboard?#{request.query_string}" }



回答2:


The simplest way to do this (at least in Rails 4) is do use the options mode for the redirect call..

get '/invoices' => redirect(path: '/dashboard')

This will ONLY change the path component and leave the query parameters alone.




回答3:


While the accepted answer works perfectly, it is not quite suitable for keeping things DRY — there is a lot of duplicate code once you need to redirect more than one route.

In this case, a custom redirector is an elegant approach:

class QueryRedirector
  def call(params, request)
    uri = URI.parse(request.original_url)
    if uri.query
      "#{@destination}?#{uri.query}"
    else
      @destination
    end
  end

  def initialize(destination)
    @destination = destination
  end
end

Now you can provide the redirect method with a new instance of this class:

get "/invoices", to: redirect(QueryRedirector.new("/dashboard"))

I have a written an article with a more detailed explanation.



来源:https://stackoverflow.com/questions/14039181/how-do-i-do-a-redirection-in-routes-rb-passing-on-the-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!