Ruby Building Web Apps with Sinatra ERB Templates URL Parameters

Robert Jenkins
Robert Jenkins
4,146 Points

How do I respond with a string reading "Hello" followed by the contents of the name URL params?

In the get route for the "/greet/:name" path, respond with a string reading "Hello" followed by the contents of the name URL parameter.

hello.rb
require "sinatra"

get "/greet/:name" do
  "Hello" params[:name]
end

2 Answers

You can use string interpolation to add params[:name] into the string. it would work like this:

require "sinatra"

get "/greet/:name" do
  "Hello #{params[:name]}"
end

Or you could just concatenate params[:name] onto the end of the string:

require "sinatra"

get "/greet/:name" do
  "Hello " + params[:name]
end
Peter Eriksson
Peter Eriksson
12,187 Points

How does params work here?

Thx