2013-07-29 47 views
0

我有一個字符串,我想添加到一個URL變量,但我不能讓Rails到而不是編碼它。無法覆蓋參數的URL編碼

下面是我得到了什麼:

<%= link_to "Example", example_path(@resource, email: '*|EMAIL|*') %> 

的是輸出爲:

http://example.com/example/123?email=%2A%7CEMAIL%7C%2A 

但我想:

http://example.com/example/123?email=*|EMAIL|* 

我已經嘗試了所有的以下至得到*|EMAIL|*輸出正確,但沒有去...

<%= link_to "Example", example_path(@resource, email: '*|EMAIL|*').html_safe %> 
<%= raw link_to "Example", example_path(@resource, email: '*|EMAIL|*') %> 
<%= link_to "Example", example_path(@resource, email: '*|EMAIL|*'.html_safe) %> 
<%= link_to "Example", example_path(@resource, email: raw('*|EMAIL|*')) %> 

回答

0

我覺得你的問題是這樣的link_to實現

def link_to(*args, &block) 
    ... 
    url = url_for(options) 

    href = html_options['href'] 
    tag_options = tag_options(html_options) 

    href_attr = "href=\"#{ERB::Util.html_escape(url)}\"" unless href 
    "<a #{href_attr}#{tag_options}>#{ERB::Util.html_escape(name || url)}</a>".html_safe 
    end 
end 

在生產線href_attr = "href=\"#{ERB::Util.html_escape(url)}\"" unless href,方法 ERB::Util.html_escape調用to_s其參它之前別的,所以無論您使用url結束越獄。設置hrefhtml_options看起來像一個出路,但tag_options也呼籲ERB::Util.html_escape

你可以只是做

<a href="<%= example_path(@resource, email: '*|EMAIL|*'.html_safe) %>">Example</a> 

我想。

1

你可以嘗試像

<%= link_to "Example", example_path(@resource) + "?email=*|EMAIL|*" %>

這也應該像

<%= link_to "Example", example_path(@resource) + "?email=*|#{@instance_var.upcase}|*" %>如果這就是你要找的事。

顯然,按照預期使用rails路徑助手會很好,但作爲最後的手段,這應該可行。

您可能還需要做些什麼管道,請參閱:

How to prevent pipe character from causing a Bad URI Error in Rails 3/Ruby 1.9.2?