简体   繁体   English

Rails 4:redirect_to:返回并更改/删除参数

[英]Rails 4: redirect_to :back and change/delete parameters

I think this should be easy but I cannot for the life of me get it to work, or find anything on the web that explains it. 我认为这应该很容易,但我不能为我的生活让它工作,或在网上找到解释它的任何东西。 After a form submit, I want to redirect back to the referer/referrer and alter or delete one or more of the query parameters. 表单提交后,我想重定向回referer / referrer并更改或删除一个或多个查询参数。

For example, the form is at 例如,表格是

/tasks/6?foo=1&bar=1

And I want to go afterwards to 我想事后去

/tasks/6?foo=0&bar=1

I tried all sorts of combinations based on 我尝试了各种各样的组合

redirect_to :back, foo: 0

or 要么

redirect_to :back, params: {foo: 0, bar: 1}

and so on, but nothing at all would redirect me to anything other than the original page. 等等,但没有任何东西可以将我重定向到原始页面以外的任何内容。 I was hoping to avoid having to mangle with the string myself, but it may come to that. 我本来希望避免不得不用字符串自行修改,但它可能会出现这种情况。

It would be awesome, too, if there were also a way to just get rid of a parameter altogether, eg redirect to: 如果还有一种方法可以完全摆脱参数,例如重定向到:它也会很棒。

/tasks/6?bar=1

Thanks! 谢谢!

:back does not accept other arguments. :back不接受其他论点。 In order to accomplish what you want to do, you need to get the value of the referrer using 为了完成您想要的操作,您需要使用引用来获取值

request.referer

or 要么

request.env['HTTP_REFERER']

Parse it with the URI library, extract the query and decompose it with Rack::Utils.parse_query . 使用URI库解析它,提取查询并使用Rack::Utils.parse_query对其进行分解。 At this point you have the hash of params. 在这一点上,你有params的哈希。

You can update the hash, and construct the URL for the redirect. 您可以更新哈希,并构造重定向的URL。

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

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