[英]How can I preserve querystring values across requests in Ruby on Rails?
I'm coming up against one of those moments working with Rails when I feel that there must be a better way than what I'm ending up with. 当我觉得必须有一个比我最终的更好的方式时,我正在反对使用Rails的那些时刻之一。
I have four querystring parameters that I want to preserve across requests through various parts of a rails app - different controllers and actions, some rendered via javascript - so that the user ends up at a URL with the same querystring parameters that they started with. 我有四个查询字符串参数,我希望通过rails应用程序的各个部分保留不同的请求 - 不同的控制器和操作,一些通过javascript呈现 - 这样用户最终会得到一个具有相同查询字符串参数的URL。
I'm finding it hard to believe that the best way is through hidden form fields and manually adding the params back in as part of a redirect_to, or using session vars for each - it just seems to un-rails like. 我发现很难相信最好的方法是通过隐藏的表单字段并手动添加params作为redirect_to的一部分,或者使用每个的会话变量 - 它似乎只是取消轨道。
Does anyone know of a better way to manage this? 有谁知道更好的方法来管理这个?
Thanks! 谢谢!
In cases like this, I'll often use a helper function to create urls based on the current set of params. 在这种情况下,我会经常使用辅助函数根据当前的params集创建url。 In other words, I'd define this: 换句话说,我定义了这个:
def merge_params(p={})
params.merge(p).delete_if{|k,v| v.blank?}
end
And then use it with url_for to create the urls for your forms and links. 然后将其与url_for一起使用,以创建表单和链接的URL。 If you need to modify and of the params, just pass them into the merge: 如果您需要修改params,只需将它们传递给合并:
# url for the current page
url_for(merge_params)
# url for a different action
url_for(merge_params(:controller => 'bar', :action => 'bar'))
# et cetera
url_for(merge_params(:action => 'pasta', :sauce => 'secret'))
This'll preserve existing params plus whatever overrides you merge in. 这将保留现有的参数以及您合并的任何覆盖。
If you want to always preserve certain parameters accross every generated URL, you can implement a default_url_parameters method in your app. 如果您希望始终在每个生成的URL中保留某些参数,则可以在应用中实现default_url_parameters方法。
This is poorly documented, but mentioned in the Rails 18n Guide 这个文档很少,但在Rails 18n指南中提到过
Implement in your application_controller.rb: 在application_controller.rb中实现:
def default_url_options(*args)
super.merge(
:foo = params[:foo],
:bar = params[:bar]
)
end
Now every URL generated by Rails will include the current request's values for foo
and bar
(unless a url is being generated that specifies those specifically to be something else). 现在,Rails生成的每个URL都将包含当前请求的foo
和bar
值(除非生成的url指定那些特别是其他的URL)。
Please note this is not a rails solution but works for me in 80% cases. 请注意,这不是一个rails解决方案,但在80%的情况下对我有用。 This is how I am able to achieve this with jQuery, obviously not the best approach. 这就是我用jQuery实现这一点的方法,显然不是最好的方法。 And I am unable to make it work past 2nd level of links: 我无法通过第二级链接使其工作:
function mergeGPS(){
var lat = getParam('lat');//localStorage.getItem('lat');
var lng = getParam('lng');//localStorage.getItem('lng');
if(lat == null || lng == null){return false;}
var links = jQuery("a");
jQuery.each(links, function(y, lnk){
//debugger;
var uri = jQuery(lnk).attr('href') + "?lat="+lat+"&lng="+lng;
jQuery(lnk).attr('href',uri);
//debugger;
})
}
function getParam(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
mergeGPS needs to be called on page load. mergeGPS需要在页面加载时调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.