rails 4 使用新参数重定向回来 [英] rails 4 redirect back with new params
本文介绍了rails 4 使用新参数重定向回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用查询字符串中的新参数重定向回引用网址?
Is it possible to redirect back to the referring url with a new param in the query string?
像这样:
redirect_to :back, custom_param='foo'
推荐答案
试试这个:
# get a URI object for referring url
referrer_url = URI.parse(request.referrer) rescue URI.parse(some_default_url)
# need to have a default in case referrer is not given
# append the query string to the referrer url
referrer_url.query = Rack::Utils.parse_nested_query(referrer_url.query).
# referrer_url.query returns the existing query string => "f=b"
# Rack::Utils.parse_nested_query converts query string to hash => {f: "b"}
merge({cp: 'foo'}).
# merge appends or overwrites the new parameter => {f: "b", cp: :foo'}
to_query
# to_query converts hash back to query string => "f=b&cp=foo"
# redirect to the referrer url with the modified query string
redirect_to referrer_url.to_s
# to_s converts the URI object to url string
这篇关于rails 4 使用新参数重定向回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文