# "link\_to" helper: Looking for no encoding in URLs

**URL:** <https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031>\
**Category:** Rails\
**Created:** [February 13, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031 "2008-02-13T17:49:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 13, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/1 "2008-02-13T17:49:48Z")

</div>

Hi you all,

I have a RESTful app where my link\_to is something like  
\<%= link\_to(“Trial”,sdrs\_query\_path(params[:id] + " = " +  
my\_transf(@value))) %\>

When click on the link, the URL that appears is:  
[http://localhost:3007/sdrs/queries/vlan\_interfaz%3DTrial](http://localhost:3007/sdrs/queries/vlan_interfaz%3DTrial)

Is there anyway to avoid this? I want the equal sign as it is in the  
URL.  
Besides, the behaviour is strange. Even though the resulting URL is  
this:  
[http://localhost:3007/sdrs/queries/vlan\_interfaz%3DTrial](http://localhost:3007/sdrs/queries/vlan_interfaz%3DTrial)

if I change it manually into the browser to this one:  
[http://localhost:3007/sdrs/queries/vlan\_interfaz=Trial](http://localhost:3007/sdrs/queries/vlan_interfaz=Trial)

it works the same. I mean, it’s great Rails encode URLs automatically,  
but if there is no need (as in this case)…

PT 1: the “manual” URL “queries/vlan\_interfaz=Trial” works even thoug  
with query params such as “queries/vlan\_interfaz=Trial?page=3”. This is  
the reason why I say here there is no need in encoding.

PT 2: With a simple it does not happen. I would use this  
way but my\_transf(@value), a method that encodes “@value”, returns the  
value as it is too, not encoded… :S

 

Thanks.

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 13, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/2 "2008-02-13T19:01:04Z")

</div>

Hi,

your solution returns  
/query/value

However, I want /query/key=value

The “to\_sym” behaves the same ☹

Thorsten M. wrote:

> this should work:
> 
> \<%= link\_to(“Trial”, sdrs\_query\_path(params[:id] =\> my\_transf(@value))  
> %\>
> 
> maybe you would have to use params[:id].to\_sym, i’m not sure, if this  
> conversion is implicit

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 13, 2008, 7:29pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/3 "2008-02-13T19:29:55Z")

</div>

maybe your routing is different…  
i used this with restful routing on an index action, that does not  
expect any additional params  
so:

\<% test = {:id =\> “foo”}%\>  
\<%= link\_to(‘Test’, played\_tracks\_path(test[:id] =\> “bar”)) %\>  
returned the correct result with /played\_tracks?foo=bar

but for the show action or any not restful routing it would fail that  
way

if you have an index action, maybe:  
sdrs\_queries\_path(…)  
would work

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 13, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/4 "2008-02-13T18:48:29Z")

</div>

this should work:

\<%= link\_to(“Trial”, sdrs\_query\_path(params[:id] =\> my\_transf(@value))  
%\>

maybe you would have to use params[:id].to\_sym, i’m not sure, if this  
conversion is implicit

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 13, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/5 "2008-02-13T19:58:22Z")

</div>

ok, i’m not sure, this is what you want (maybe you should post some more  
info about your routing etc)

routes.rb:  
map.resources :play\_entries

view:  
\<% test = {:id =\> “foo”}%\>  
\<% value = “bar”}%\>  
\<%= link\_to(‘Test’, play\_entry\_path(test[:id] + “=” + value)) %\>

generates:

[ 

which would call the show action in my case…

](https://www.ruby-forum.com/play_entries/foo=bar)

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 13, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/6 "2008-02-13T20:56:12Z")

</div>

I was doing that, but the result of link\_to was:

Thorsten M. wrote:

> ok, i’m not sure, this is what you want (maybe you should post some more  
> info about your routing etc)
> 
> routes.rb:  
> map.resources :play\_entries
> 
> view:  
> \<% test = {:id =\> “foo”}%\>  
> \<% value = “bar”}%\>  
> \<%= link\_to(‘Test’, play\_entry\_path(test[:id] + “=” + value)) %\>
> 
> generates:
> 
> [ 
> 
> which would call the show action in my case…
> 
> ](https://www.ruby-forum.com/play_entries/foo=bar)

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 13, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/7 "2008-02-13T19:40:08Z")

</div>

Um…your problem is different

I want to do something like query/:id  
where :id may be “key=value”  
You see? The entire url is a path URL, not a query URL.

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 14, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/8 "2008-02-14T12:41:26Z")

</div>

On 13 Feb 2008, at 16:49, Damaris F. wrote:

> PT 1: the “manual” URL “queries/vlan\_interfaz=Trial” works even thoug  
> with query params such as “queries/vlan\_interfaz=Trial?page=3”. This  
> is  
> the reason why I say here there is no need in encoding.

= is a reserved character in paths according to

> **[An example HTML document](https://www.rfc-editor.org/rfc/rfc2396.txt)**

3.3  
which probably means you shouldn’t use it like that. ‘It works’ isn’t  
usually a good test for these sort of things. You may be relying upon  
quirks of one particular web browser, server etc…

Fred

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 14, 2008, 1:55pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/9 "2008-02-14T13:55:19Z")

</div>

Hi all,  
yes, you’re right, and I can try with another character.  
The thing is that, for example, the character “|” is not a reserved  
character in paths (I think), but Rails also encodes it.

Apart from that, there are a lot of apps that use these kind of  
characters. See google, when you search for an image:  
[http://images.google.es/imgres?imgurl=http://www.lightworker.com/album/indexthumbs/alien\_hello.gif&imgrefurl=http://profile.myspace.com/index.cfm%3Ffuseaction%3Duser.viewprofile%26friendid%3D76552036&h=300&w=300&sz=50&hl=es&start=3&um=1&tbnid=YRZ9kSX973HXCM:&tbnh=116&tbnw=116&prev=/images%3Fq%3DHello%26um%3D1%26hl%3Des%26sa%3DN](http://images.google.es/imgres?imgurl=http://www.lightworker.com/album/indexthumbs/alien_hello.gif&imgrefurl=http://profile.myspace.com/index.cfm%3Ffuseaction%3Duser.viewprofile%26friendid%3D76552036&h=300&w=300&sz=50&hl=es&start=3&um=1&tbnid=YRZ9kSX973HXCM:&tbnh=116&tbnw=116&prev=/images%3Fq%3DHello%26um%3D1%26hl%3Des%26sa%3DN)

after the [http://images.google](http://images.google)… there is a “imgurl=http://…” I mean,  
here they also use the character “/”, and it is not encoded.

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 14, 2008, 10:21am UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/10 "2008-02-14T10:21:04Z")

</div>

Damaris F. wrote:

> I was doing that, but the result of link\_to was:

it worked in my case.  
did you copy my example?

maybe it has to do with the routing and the kind of action. eg my  
example worked on the index action, which does not need an id parameter.  
your sdrs\_query\_path (singular path) should call the show action and  
therefore expect the id as first parameter. in this case it may handle  
things different.

or there are differences in the rails versions? i simply can’t reproduce  
it in the way it works for you…

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 14, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/11 "2008-02-14T13:57:29Z")

</div>

My Rails version is 1.2.5. I can reproduce your example programming an  
“\<a href=…” directly, but not with link\_to. Link\_to always encode my  
characters.

Thorsten M. wrote:

> Damaris F. wrote:
> 
> > I was doing that, but the result of link\_to was:
> 
> it worked in my case.  
> did you copy my example?
> 
> maybe it has to do with the routing and the kind of action. eg my  
> example worked on the index action, which does not need an id parameter.  
> your sdrs\_query\_path (singular path) should call the show action and  
> therefore expect the id as first parameter. in this case it may handle  
> things different.
> 
> or there are differences in the rails versions? i simply can’t reproduce  
> it in the way it works for you…

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 14, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/12 "2008-02-14T15:06:13Z")

</div>

Yes, I thought I wouldn’t have problems cause I am not using the “?”, so  
the “=” sign was not involved in any query variable ☹ But well…

Xavier N. wrote:

> On Feb 14, 2008, at 13:55 , Damaris F. wrote:
> 
> > yes, you’re right, and I can try with another character.  
> > The thing is that, for example, the character “|” is not a reserved  
> > character in paths (I think), but Rails also encodes it.
> 
> In fact I am not 100% sure it is technically a reserved character  
> there. Let me explain.
> 
> The RFC says you first need to split the URL apart, and then interpret  
> reserved characters _depending on the part of the URL you are dealing  
> with_. My interpretation as far as “=” is concerned is that first you  
> would split the URL by “?” and then interpret “=” in the query string.  
> Since AFAIK there’s no special meaning for “=” to the left of the  
> question mark I think technically it is not required to be escaped.

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [February 14, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/link-to-helper-looking-for-no-encoding-in-urls/129031/13 "2008-02-14T14:15:05Z")

</div>

On Feb 14, 2008, at 13:55 , Damaris F. wrote:

> yes, you’re right, and I can try with another character.  
> The thing is that, for example, the character “|” is not a reserved  
> character in paths (I think), but Rails also encodes it.

In fact I am not 100% sure it is technically a reserved character  
there. Let me explain.

The RFC says you first need to split the URL apart, and then interpret  
reserved characters _depending on the part of the URL you are dealing  
with_. My interpretation as far as “=” is concerned is that first you  
would split the URL by “?” and then interpret “=” in the query string.  
Since AFAIK there’s no special meaning for “=” to the left of the  
question mark I think technically it is not required to be escaped.
