Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9227571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:06:46+00:00 2026-06-18T05:06:46+00:00

I’ve just started my first Sinatra project, a simple TV-show management web app, and

  • 0

I’ve just started my first Sinatra project, a simple TV-show management web app, and wanted to have beautiful URLs. So when a user types in the search box and submits, I don’t want to have a /?search=foobar-style URL and want do redirect them to /search/foobar. This also enables me to separate the get '/search/:name route from the main get '/' route. I’ve implemented the redirect using a before filter and properly escaped the params[] variable:

before do
    if params.has_key? 'search'
        redirect to("/search/#{URI.escape(params['search'])}")
    end
end

and later I continue with

get '/search/:query' do
    result = search_api params[:query]
    if result == 'null'
        # no results
    else
        result = JSON.parse(result)
        if result.key? 'shows'
            # display search results
        else
            # redirect to one single show
            # (result.keys).first is the real name of the show provided
            # by the api. It may contain special characters
            #
            # (result.keys).first #=> "Breaking Bad"
            # result.keys #=> "Breaking Bad"
            # result.key? "Breaking Bad" #=> true
            redirect to('/show/#{URI.escape((result.keys).first)}')
        end
    end
end

unfortunately, the redirect to the /show page only works, if there are no URI special characters in the name apart from %. This also means no spaces. When I search for something with a space or an umlaut or anything, e.g. a GET for /?search=Breaking%20Bad, I get the following error from Sinatra/Rack:

[2013-02-02 00:30:29] ERROR URI::InvalidURIError: bad URI(is not URI?): http://localhost:9393/show/Breaking Bad
    /Users/Ps0ke/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/generic.rb:1202:in `rescue in merge'
    /Users/Ps0ke/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/generic.rb:1199:in `merge'
    /Users/Ps0ke/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpresponse.rb:220:in `setup_header'
    /Users/Ps0ke/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpresponse.rb:150:in `send_response'
    /Users/Ps0ke/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:110:in `run'
    /Users/Ps0ke/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

The browser shows me, that it got redirected to /search/Breaking%20Bad, so the first redirect worked. This “bug” only occurs, when the search produces an exact hit, so the problem with the redirect is to search in the get '/search/:query' route. I remember that it worked once, but can’t find the right commit in my git history.

I am running

% gem list sinatra
sinatra (1.3.4, 1.3.3)

% gem list rack
rack (1.4.4, 1.4.1)
rack-cache (1.2)
rack-flash3 (1.0.3)
rack-protection (1.3.2, 1.2.0)
rack-ssl (1.3.2)
rack-test (0.6.2, 0.6.1)

Maybe someone of you can tell me:

  1. whether this is good or bad practices/there is another way in sinatra to prettifiy URLs
  2. how to fix this, since I think I have already escaped everything carefully

Thank you very much in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:06:48+00:00Added an answer on June 18, 2026 at 5:06 am

    In the line

    redirect to('/show/#{URI.escape((result.keys).first)}')
    

    you are using single quotes. This means that string interpolation isn’t performed, so the literal string /show/#{URI.escape((result.keys).first)} is being used as the url and that is why it is failing.

    In order for interpolation to work you need to use double quotes like this:

    redirect to("/show/#{URI.escape((result.keys).first)}")
    

    This will cause #{URI.escape((result.keys).first)} to be replaced with the escaped movie name, which should be a valid url.

    Note that in your first redirect you do use double quotes, so it works as expected:

    redirect to("/search/#{URI.escape(params['search'])}")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.