Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra Url '/' interpretations

Tags:

url

ruby

sinatra

I am a ruby newbie and have been trying Sinatra for quite some time now, one thing that Iam not able to figure out is why does a '/' in the url make such a big difference. I mean isnt:

get 'some_url' do 
end

and

get 'some_url/' do
end

Supposed to point to the same route? why is that Sinatra considers it as different routes? I spent a good one hour trying to figure that out.

like image 915
djd Avatar asked Oct 09 '22 20:10

djd


2 Answers

According to RFC 2616 and RFC 2396 (RFCs defining resource identity) those URLs do not define the same resource. Therefore Sinatra treats them differently. This is esp. important if you imagine the route returning a page with relative links. This link

<a href="bar">click me</a>

Would point to /bar if you're coming from /foo, to /foo/bar if you're coming from /foo/.

You can use the following syntax to define a route matching both:

get '/foo/?' do
  # ...
end

Or the Regexp version mentioned in the comments above.

like image 184
Konstantin Haase Avatar answered Oct 13 '22 11:10

Konstantin Haase


They are different routes. The second is a URL with a directory extension ('/'); the first is a URL with no extension. A lot of frameworks (like Rails) will interpret both as the same route, or append the `/' (e.g., Django, and Apache can be configured to do that as well), but technically they are different URLs.

like image 36
mipadi Avatar answered Oct 13 '22 09:10

mipadi