简体   繁体   English

替换redirect_to request.referer中的GET参数

[英]Replace GET params in redirect_to request.referer

I'm trying to Replace GET params in: 我正在尝试替换GET参数:

redirect_to request.referer

My request.referer already contains one parameter: 我的request.referer已经包含一个参数:

http://www.foo.com/bar?par=10

When i try: 当我尝试:

redirect_to request.referer, :par => 5

it doesn't work. 它不起作用。 It redirects to referer but doesn't change :par from 10 to 5. When i do redirect to url_path, eg 它重定向到引用,但不会更改:par从10到5。当我确实重定向到url_path时,例如

redirect_to root_path, :par => 5

This works ok, redirects to: 可以,可以重定向到:

http://www.foo.com/?par=5

So my question is how to replace params in request.referer URI. 所以我的问题是如何替换request.referer URI中的参数。 Additional question is whether should I use request.referer or :back ? 另一个问题是我应该使用request.referer还是:back?

Thanks 谢谢

The problem is that redirect_to ultimately just takes a string, ie the url. 问题是redirect_to最终只接受一个字符串,即url。 If you were to do something like 如果你要做类似的事情

redirect_to edit_foo_path(@foo, :bar => "qux")

then you're using a path helper to generate that string. 那么您正在使用路径助手来生成该字符串。 ie, edit_foo_path(:bar => "qux") is the helper and it will be converted to "/foo/123/edit?bar=qux" which is just a "dumb" string. 即, edit_foo_path(:bar => "qux")是帮助器,它将被转换为"/foo/123/edit?bar=qux" ,它只是一个“哑”字符串。 If you were working with the helper you can switch the params around but with the string it's already finished, if you know what i mean. 如果您正在使用帮助程序,则可以切换参数,但是使用字符串已经完成,如果您知道我的意思。

request.referer is a string as well, so what you'll need to do is to break it down into its constituent parts, modify those parts as required, and then reassemble it into a string again. request.referer也是一个字符串,因此您需要做的是将其分解为组成部分,根据需要修改这些部分,然后再次将其重新组装为字符串。 The parts in question are protocol, host, path & params. 有问题的部分是协议,主机,路径和参数。 You don't need to change the protocol, host or path in this case so you can keep them the same. 在这种情况下,您无需更改协议,主机或路径,因此可以使其保持不变。 params will be most easily manipulated when converted to a hash. 将参数转换为哈希后,将最容易对其进行操作。 Rails has various url-processing functions which you can use here, so there's probably a few different ways of doing this. Rails具有各种url处理功能,您可以在此处使用它们,因此可能有几种不同的方法可以执行此操作。 I would do this like follows, which probably isn't the most efficient. 我将按照以下步骤进行操作,这可能不是最有效的。

url = URL(request.referer)
#you could get this via a regex but i'm doing it "formally" with the Url object
host_and_path = "#{url.scheme}://#{url.host}#{url.path}"
params = CGI.parse(url.query)
#now you've got params as a hash you can do what you want to it.
params["par"] = 5
new_url = "#{host_and_path}?#{params.to_param}"
redirect_to new_url

like i say there's probably more efficient (in terms of lines of code, there's no issues with it speed-wise) ways to do this, but it's useful to see the step-by-step approach anyway i guess. 就像我说的那样,可能有更有效的方法(就代码行而言,它没有速度方面的问题),但是无论如何,我认为逐步查看方法很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM