gpt4 book ai didi

ruby-on-rails - Rails 不断将我的字符串 "?"更改为 "%3F"

转载 作者:数据小太阳 更新时间:2023-10-29 08:54:36 25 4
gpt4 key购买 nike

基本上我只想在辅助方法中插入这个 + "?direction=desc"

但是一旦它解析出来,它就会像这样..

/organizations/search?order_by=contactable%3Fdirection%3Ddesc

有人知道解决这个问题的方法吗?

我的助手方法:

def search_sort(name, sort_by, order = 'asc')
link_to(name, url_for(:overwrite_params => { :order_by => sort_by + "?direction=desc" :page => nil }), :class => 'selected save_pushstate')
...

我知道你在想什么。只需添加 :order 即可。问题是我正在使用来自 #175 of railscasts 的 AJAX 历史保护程序。

$(".save_pushstate").live("click", function() {
$.setFragment({"order_by" : $.queryString($(this).attr('href')).order_by});
//$.setFragment({"direction" : $.queryString($(this).attr('href')).direction});
return false;
});

它将我的 url 重写为一个“片段”。我不能有两个!所以我决定,如果我可以在硬编码的 href 中添加方向参数,它就可以处理整个困惑。

最佳答案

尝试:

+ "?direction=desc".html_safe

编辑:由于您使用的是 rails 2.3.5,请尝试以下操作:

def search_sort(name, sort_by, order = 'asc')
link_to(name, url_for(:overwrite_params => { :order_by => sort_by + "?direction=desc" :page => nil }, :escape => false), :class => 'selected save_pushstate')
...

注意 url_for 中的“:escape => false”。

编辑2:读完后: http://www.ruby-forum.com/topic/80381

特别是这段摘录:

I think this is where the confusion is arising. There are two different kinds of escaping going on.

It sounds like you're talking about the URL encoding that uses '%xx' to represent special characters.

However, the html_escape function does something completely different. It takes a string and turns '&' into '&' and '<' into '<', etc., so that it can go into HTML without being interpreted as literal '&'s and '<'s.

Escaping special characters in URLs using the '%xx' scheme is mandatory, otherwise they are not valid URLs.

我已经意识到您看到的“转义”是 url 编码,它不应该影响您的查询/排序等。您可以通过获取编码的 url 并将其输入浏览器来测试它。

:escape => false 禁用 html 转义,这意味着危险字符被转换为显示代码,例如 '&' 转换为 '&' 和 '<' 转换为 '<' 等,

还有“?”在你的追加中应该是“&”:

+ "&direction=desc"

希望这对您有所帮助。 =)

关于ruby-on-rails - Rails 不断将我的字符串 "?"更改为 "%3F",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4683321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com