Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnas Rida
8,183 Pointswhy #{params["title"]}
I understand that in ruby code we use the #{}
to get the value of something, but in an earlier video we used the following code:
get "/:title" do
@title = params[:title]
@content = page_content(@title)
erb :show
end
why is it that in the URI.escape()
method we used #{params["title"]}
and not just params[:title]
??
1 Answer
Maciej Czuchnowski
36,441 PointsString interpolation - #{}
- is used to put a value of something (like a variable or ruby code) INSIDE a string.
So, for example, if I want a string to dynamically change depending on the value of @[Jimmy Names](https://teamtreehouse.com/names)
variable value, I'd write "Hello #{@name}"
, since writing "Hello @name"
would not work.
So, back to your question, you will notice that these URI.escape()
take a string as a parameter. If we only pass params[:title]
there, we're still missing the /
part of the URL. We want the full string to consist of /
symbol and our param. So we make a string and inside we interpolate the value of params[:title]
, so that the final result would be the /
symbol, followed by whatever is in the params title key.