Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Building Web Apps with Sinatra ERB Templates Embedding Instance Variables

Osvaldo Gonzalez
Osvaldo Gonzalez
5,340 Points

Back in hello.rb, at the end of the route block, render the views/hello.erb template. What am I doing wrong?

Please Help

require "sinatra"
set :bind, ""

def page_content(title)
rescue Errno::ENOENT
  return nil
get "/greet/:name" do
  @name = params[:name]
  @content = content_page(@name)
  erb :show
require 'views/hello.erb' 

get '/' do
  erb :views/hello
<p>Hello <%= @name %></p>

2 Answers

This confused me too, but I found that this completed the challenge:

erb :hello
Jay McGavren
Jay McGavren
Treehouse Teacher

You may have misunderstood how the code is supposed to work, here. The goal is to let the user visit a path like /greet/Osvaldo or /greet/Jay, take the second portion of the path (Osvaldo or Jay), and display a greeting based only on that path (<p>Hello Osvaldo</p> or <p>Hello Jay</p>).

So this code you have is a good use of URL parameters. This is all you need to do to retrieve the name, you don't need to define or call a page_content method.

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

But you need to add an end keyword to close the route's block. Also, get "/greet/:name" is the only route you need; you don't need a get '/' route.

I would recommend going back and reviewing the video on ERB templates. The info you need to render the template should be there.