Rails keeps changing my string "?" into "%3F"
Basicaly I just want to insert this + "?direction=desc"
in helper method.
But once it parses it comes out like this..
/organizations/search?order_by=contactable%3Fdirection%3Ddesc
Anyone know a way around this?
My Helper Method:
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')
...
I know what you're thinking. Just add :order into it. The problem being is that I 'm using an AJAX history saver from #175 of railscasts.
$(".save_pushstate").live("click", function() {
$.setFragment({"order_by" : $.queryString($(this).attr('href')).order_by});
//$.setFragment({"direction" : $.queryString($(this).attr('href')).direction});
return false;
});
And it rewrites my url to just one "fragment". I can't have two! So I decided that if I can just add the direction param in the开发者_运维问答 href hard-coded, it could deal with this whole mess.
Try:
+ "?direction=desc".html_safe
Edit: Since you're using rails 2.3.5, try this:
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')
...
Note the ":escape => false" in url_for.
Edit2: After reading this: http://www.ruby-forum.com/topic/80381
Specifically this excerpt:
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.
I've realized that the 'escaping' that you see happening is url encoding, and it shouldn't affect your query/sorting, etc. You can test it out by taking the encoded url and typing it into your browser.
:escape => false disable html escaping, which means dangerous characters get converted to display codes, such as '&' into '&' and '<' into '<', etc.,
And the "?" in your append should be "&":
+ "&direction=desc"
Hope this helps. =)
精彩评论