开发者

Rails - appending query parameters to existing URL

开发者 https://www.devze.com 2023-03-04 17:05 出处:网络
I have an application controller method called redirect back or default which is used to redirect users to the page they were requesting after login

I have an application controller method called redirect back or default which is used to redirect users to the page they were requesting after login

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

I would like to be able to optionally add URL parameters (for some analytics tracking) to the url, but am not sure of the best way. I'd like to change the method signature to this

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

and somehow attach the params to the existing URL. Is there a standard ruby or ROR wa开发者_如何转开发y to do this? I could obviously brute force check to see if there is a query string as part of the URL with regex and manually build the query string, but I was hoping there is an easier standard way of doing this.


From here:

To pass parameters with redirect_to you simply add them. Like ...

 redirect_to :controller => 'another', :action => 'def', :param1 => 'some', :param2 => 'thing', :param => 'else'


standart approach

def redirect_to_back_or_default(default = "/")
  back = case request.env["HTTP_REFERER"]
  when request.fullpath
    default
  when nil
    default
  else
    :back
  end
  redirect_to back
end
0

精彩评论

暂无评论...
验证码 换一张
取 消