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 trial

Ruby Building Web Apps with Sinatra ERB Templates Embedding Instance Variables

How does erb knows the path of the file which sits in another folder

Hi Everyone,

when executing the following:

get "/greet/:name" do @name = params[:name] erb :hello end

How does erb knows the path of hello.erb located in the views folder if we don't mention it?

hello.rb
require "sinatra"

get "/greet/:name" do
  @name = params[:name]
  erb :hello
end
views/hello.erb

1 Answer

Dillon Wyatt
Dillon Wyatt
5,990 Points

A bit of sinatra / rails / ruby magic.

In rails controllers (that's half of the hello.rb above) and views are linked. Behind the scene. A function that simple looks for file directories with specific naming patterns and taking in the limited variables you give it is run. So ERB here is basically taking the ":hello" bit you pass it and running a method that looks for the "hello.erb" file in a pre-determined location of app/views or some such.