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.
Excellent advice Igor. Keep it up!
LikeLike