Search code examples
rubysinatraerb

How to pass an instance variable to a erb template in a Sinatra route?


How do I pass an instance variable to an erb template in a Sinatra route. The below doesn't work.

   get '/' do
    
       @beercompany = "Coors"
       erb :mytemplate
    end 

mytemplate.erb

<h1> Hello from <% @beercompany %></h1>

output doesn't show beercompany's name

Hello from 

Solution

  • You need to put = to display the variable:

    <h1> Hello from <%= @beercompany %></h1>