Making Sinatra routes insensitive to the trailing backslash

Today I’m going to talk about how we make Sinatra routes insensitive to the trailing backslash. This trick will help you if you just got started with Sinatra.

Now that you’re creating RESTful apps and decided that Sinatra it’s your chosen one framework, it’s very important that you always end your routes with a “/?”. It’s important not to forget that Sinatra accepts regular expressions on their routes. If you create a simple route like this:

[ruby]
get "/twitter-posts" do
# some code
haml :index, :format => :html5
end
[/ruby]

Sinatra will not understand the route if typed like this: “/twitter-posts”. With that in mind, it’s very important that you always end up your routes like this:

[ruby]
get "/twitter-posts/?" do
# some code
haml :index, :format => :html5
end
[/ruby]

Now, you’re telling Sinatra that your route has a optional trailing backslash and now will understand both with or without trailing backslash.

One thought on “Making Sinatra routes insensitive to the trailing backslash

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: