Escaping characters in links

I have the below link I am generating and returns some
strings with a forward slash ("/"). This of course messes up routing.
I would like to escape it to %2F however I am unclear on how to do

<%= link_to, types_path(CGI::escape( %>

If I do the below. it gives a no route error.

<%= link_to, types_path(CGI::escapeHTML( %>


hi, have a try:
<%= link_to, “#{types_path(}” %>

keep the second parameter is string,right?

2011/5/21 tashfeen.ekram [email protected]

Deshi Xiao
Twitter: xds2000
E-mail: xiaods(AT)

On Friday, May 20, 2011 4:48:17 PM UTC-6, tashfeen.ekram wrote:

<%= link_to, types_path(CGI::escapeHTML( %>


The #types_path method is an auto-generated “named routes” helper (at
I’m assuming it is based on its name and how you’re using it). If you
nave a
resource route such as: “resources :types” in your config/routes.rb then
#types_path helper doesn’t need any arguments and takes you to the index
action on the TypesController. So, if my assumptions are right and the
action is what you’re trying to link to, then the following should be

<%= link_to, types_path %>

However, if you’re trying to link to a specific instance (which it kind
looks like you intend to) then you’d instead use:

<%= link_to, type_path(type) %>

Anyhow, update us on whether these assumptions are correct or not if
still having problems.

That is did not seem to work. I got a routing error.

<%= link_to CGI::escape(, types_path(CGI::escape(

The name of the link is appropriately displayed with the “/” replaced
but the link is not.

On Friday, May 27, 2011 3:41:00 PM UTC-6, tashfeen.ekram wrote:

actually, it is not a resource route. it is a route that is
specifically defined. that is there is not resource “type.”

match “/types/:types” => “browse#list”, :as => :types

It is unresourceful route.

If this is your route, as long as you don’t have other routes that begin
with “/types/…” then you could update it with a constraint that allows
forward slashes:

match “/types/:types” => “browse#list”, :types => /.*/, :as => :types

This should both make the routes match correctly on incoming URLs and
allow you to call: types_path(“text/with/slashes”) without any errors

Of course, you should adjust the constraints regexp as is appropriate (I
find it ugly/hackish to have it wide open like that except where it
truly make sense). Also, if you had other routes like:

match “/types/sub/directory/:id” => “browse#yourmom”, :as => :your_mom

Then it’d conflict using the wide-open constraint technique.

Anyhow, if you absolutely insist on simply escaping slashes instead of
adding a constraint to your route, what was wrong with your first,

<%= link_to, types_path(CGI::escape( %>

You mentioned the second one gave a “no route” error, but did this one?
just did a quick test and this actually worked for me). Just curious.

actually, it is not a resource route. it is a route that is
specifically defined. that is there is not resource “type.”

match “/types/:types” => “browse#list”, :as => :types

It is unresourceful route.

<%= link_to, types_path(CGI::escape( %>

The above one renders the page fine. However, when I click on the
link, then it gives me the no route error.

<%= link_to, types_path(CGI::escapeHTML( %>

The one above does not even render the page and gives me the below

No route matches {:controller=>“xxx”, :types=>“yyy/
yyy”, :action=>“zzz”}

Also, I noticed that even spaces are not being escaped with the below:
<%= link_to, types_path(CGI::escapeHTML( %>

The link is rendered with a space in it. Once I click on it, it is
eventually rerouted to the same address except with the space escaped
with a %20. That does not seem like the intended behavior?

Even the below does the same thing:

<%= link_to “blah”, “/types/Vits%2Fmins” %>

In both of the below, the explicit escaped back slash “%2F” is
converted to an actual backslash.

<%= link_to “blah”, types_path(“Vits%2Fmins”) %>
<%= link_to “blah”, :controller => :browse, :action => :list, :types
=> “Vits%2Fmins” %>

Is this a safety feature of the link_to helper?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs