将参数值作为 rails 中的查询字符串传递给 redirect_to [英] Passing param values to redirect_to as querystring in rails

查看:48
本文介绍了将参数值作为 rails 中的查询字符串传递给 redirect_to的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很简单,但我似乎找不到简单的答案.

This should be simple, but I can't seem to find an easy answer.

如何将当前请求中的参数值传递到 redirect_to 调用中?我有一些表单值我想传递到 GET 重定向的查询字符串中

How can I pass param values from the current request into a redirect_to call? I have some form values I'd like to pass into the query string of a GET redirect

我想做类似的事情:

redirect_to @thing, :foo => params[:foo]

并发送至:

http://things/4?[foo][key1]=val1&[foo][key2]=val2

谢谢!

另外 - 如何为 redirect_to :back 处理这个问题?

Also - how could this be handled for a redirect_to :back?

redirect_to :back, :foo => params[:foo]

推荐答案

redirect_to 仅将第二个参数用于响应状态.您必须使用另一种形式的 redirect_to,例如字符串"形式.例如:

The 'Record' form of redirect_to uses the second argument only for the response status. You'll have to use another form of redirect_to, like the 'String' form. e.g.:

redirect_to thing_path(@thing, :foo => params[:foo])

这将适用于您提到的嵌套 params[:foo] 参数.或者,正如 Drew 在下面评论的那样,您可以使用 polymorphic_url(或 _path):

which will work for nested params[:foo] params like you mentioned. Or, as Drew commented below, you can use polymorphic_url (or _path):

redirect_to polymorphic_path(@thing, :foo => params[:foo])

这篇关于将参数值作为 rails 中的查询字符串传递给 redirect_to的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆