Escaping characters in links

I have the below link I am generating and type.name 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
that.

<%= link_to type.name, types_path(CGI::escape(type.name)) %>

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

<%= link_to type.name, types_path(CGI::escapeHTML(type.name)) %>

Thanks.

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

keep the second parameter is string,right?

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

<%= link_to CGI::escape(type.name), types_path(CGI::escape(type.name))
%>

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

The #types_path method is an auto-generated “named routes” helper (at least 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 the #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 index action is what you’re trying to link to, then the following should be sufficient:

<%= link_to type.name, types_path %>

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

<%= link_to type.name, type_path(type) %>

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

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.

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 being raised.

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 would 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, original example:

<%= link_to type.name, types_path(CGI::escape(type.name)) %>

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

<%= link_to type.name, types_path(CGI::escape(type.name)) %>

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

<%= link_to type.name, types_path(CGI::escapeHTML(type.name)) %>

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

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 type.name, types_path(CGI::escapeHTML(type.name)) %>

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?

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?

Even the below does the same thing:

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