Slash in URL Parameter

Has anyone come across a problem whereby a parameter in the
urn, :name, for example, contains a slash - either escaped (%2F) or
not (/) that causes the route to be incorrectly recognised?

I have the following route:

“climbs/:id/:name/:action”, :action => ‘show’, :id => /(\d*)/

If the :name part contains a slash, the route is not followed. If
anyone else has seen this, I’d love to know how you sorted it!

Thanks,

Matt

Matt H. wrote:

Has anyone come across a problem whereby a parameter in the
urn, :name, for example, contains a slash - either escaped (%2F) or
not (/) that causes the route to be incorrectly recognised?

I have the following route:

“climbs/:id/:name/:action”, :action => ‘show’, :id => /(\d*)/

If the :name part contains a slash, the route is not followed. If
anyone else has seen this, I’d love to know how you sorted it!

Thanks,

Matt

/ is considered a separator character by the routing engine. That is
how it divides params.

I know you can get around this with the “.” character by using a
requirement regex. So maybe you can do the same with the “/”

“climbs/:id/:name/:action”, :action => ‘show’, :id => /(\d*)/,
:name => %r{/?}

It may not work though. With this regex it will probably always
interpret the :action as part of the :name unless :name always had a
slash in it.

You sure you wouldn;t be better off with a ‘-’ instead of a ‘/’ in that
:name?

Hi Alex,

Thanks for replying - it’s a bit of a bind, this one!

The name part is specified by the users, so it’s not possible to
guarantee that it will always or never contain a slash. I was hoping
that %2F would be interpreted by routing as being part of a parameter,
whereas a literal / would be interpreted as a separator.

I’m sure this has been solved by someone somewhere - how would a rails-
based blog cope with a post that contains a slash in the title, for
example?

Thanks again.

Matt H. wrote:

Hi Alex,

I’m sure this has been solved by someone somewhere - how would a rails-
based blog cope with a post that contains a slash in the title, for
example?

Thanks again.

I’d have thought that it wouldn’t. Since as a separator / is part of
the URL spec, I think it’s just not going to work. You can encoide
slashes as part of the query string, but not (I’d have thought) as part
of the URL… any URL, not just rails.

My suggestion would be to just not allow slashes as part of the name, or
replace them with ‘-’ when used as part of the url…which is what
usually happens when a permalink is generated from a post name.

Alan

Matt H. wrote:

So the solution would be to create a slug column in any table that
allows data from a column that contains slashes or periods (same
problem with periods in rails >= 1.2) - the slug column containing an
appropriately escaped value of the column to be referenced in the URL.

I’ll give this a try.

Thanks for your help!

On 22 Feb, 15:12, Alan F. [email protected]

Either that or do the substitution on the fly (both on the way out and
the way in).

A.

So the solution would be to create a slug column in any table that
allows data from a column that contains slashes or periods (same
problem with periods in rails >= 1.2) - the slug column containing an
appropriately escaped value of the column to be referenced in the URL.

I’ll give this a try.

Thanks for your help!

On 22 Feb, 15:12, Alan F. [email protected]

Matt H. wrote:

Has anyone come across a problem whereby a parameter in the
urn, :name, for example, contains a slash - either escaped (%2F) or
not (/) that causes the route to be incorrectly recognised?

I have the following route:

“climbs/:id/:name/:action”, :action => ‘show’, :id => /(\d*)/

If the :name part contains a slash, the route is not followed. If
anyone else has seen this, I’d love to know how you sorted it!

Thanks,

Matt

I just have the same problem.
to solve it, i encode the parameter

put in the controller header :
require ‘cgi’
before send encode parameter :
CGI::escape(params[:name])
at reception decode it :
CGI::unescape(params[:name])

Then, you’ll keep valid url :slight_smile: