Why have ampersands avoided when creating a URL using link_to?

Here are my simple 3 code rails:

<%= link_to "link", gateway_index_url(developer:@item.developer.api_key, tracker:"email", url:@product.url) %> 

And the result:

 <a href="/gateway?developer=abcde&amp;tracker=email&amp;url=http%3A%2F%2Fwww.bla.fr%2FproductA" >link</a> 

The problem is that & rewritten in &amp; . I can't figure out how to prevent escaping since :escape => false does not exist in Rails 3

+6
source share
2 answers

Update: So, the source

  def link_to(*args, &block) if block_given? options = args.first || {} html_options = args.second link_to(capture(&block), options, html_options) else name = args[0] options = args[1] || {} html_options = args[2] html_options = convert_options_to_data_attributes(options, html_options) 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 

As we can see, this design behavior is from the source.

You can try one of two solutions, I have not tried them, but they should work

1.) Try placing the call on the gateway inside the call on #raw:

 <%= link_to "link", raw(gateway_index_url(developer: @item.developer.api_key, tracker:"email", url:@product.url)) %> 

This may solve your specific problem, a second approach, while a slightly brighter force should also work ...

2.) If you want to convert it (all href) back, you can use CGI :: unescape_html:

 <%= CGI::unescape_html(link_to "link", gateway_index_url(developer: @item.developer.api_key, tracker:"email", url:@product.url)) %> 

Good luck, hope this helps.

Update 2: Fixed call to cgi unescape, used ".". when it should be "::" and the formatting fix. Forgot indent example for # 1

+6
source

Rory O'Kane is in place. Answer to the question "Why did ampersands avoid when creating a URL using link_to?" this is the right way to highlight parameters in a url.

Is there a problem with the url as it is? If so, could you talk about the problem?

Perhaps you can prevent URL escaping by using raw on the entire URL:

 <%= link_to "link", raw(gateway_index_url(developer:@item.developer.api_key, tracker:"email", url:@product.url)) %> 
0
source

Source: https://habr.com/ru/post/952301/


All Articles