Question

I have a Sinatra application with the following GET method that takes the URL passed in:

get  %r{/html/(.+)}  do 
  url = params[:captures] # stores url => http://www.example.com

  gethtml(url)
end

However, when gethtml(url) is called, it raises the error Sinatra no implicit conversion of Array into String.

gethtml accepts input such as http://example.com.

I know this is a data-type conversion issue and I tried calling to_s but it did not work.

Any help would be appreciated.

Was it helpful?

Solution

params[:captures] returns an array of strings, while get_html most likely accepts one URL as string.

Since you want to use the first group that matches as the URL:

get %r|/html/(.+)| do
  get_html params[:captures].first
end

This is consistent with the Route matching with Regular Expressions example in the Routes section of the README.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top