Multiple Block Parameters with Sinatra
Question
I'm trying to get this Sinatra GET request to work:
get '/:year/:month/:day/:slug' do
end
I know you can get one param to work with block parameters:
get '/:param' do |param|
"Here it is: #{param}."
end
But how can I use multiple block parameters with the first code block? I'm open to other methods.
Solution
Multiple placeholders are stored in params
as Hash.
# Request to /2009/10/20/post.html
get '/:year/:month/:day/:slug' do
params[:year] # => 2009
params[:month] # => 10
params[:day] # => 20
params[:post] # => post.html
end
OTHER TIPS
Forgive my ignorance of Sinatra, but shouldn't this set named parameters like Rails map.connect
?:
get '/:year/:month/:day/:slug
Now the parameters should be accessible in the params
hash:
params = { :year => "foo", :month => "bar", :day => "baz", :slug => "etc" }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow