rubysinatrapadrino

Ruby/Sinatra: Passing a URL variable to an .erb template


I'm using Padrino, and I want to take parameters out of URL and use them in an .erb template.

In my app setup I have:

get '/testpage/:id' do
  userID = params[:id]
  render 'test/index'
end

In my test/ folder I have index.html.erb which is successfully rendered, for a url like http://localhost:9000/testpage/hello123.

However, I've tried printing the params[:userID] on the page with:

<%= @userID %>

The rest of the page renders fine but hello123 isn't anywhere to be found. When I try <%= userID %> I get undefined local variable or method `userID' for #<stuff>

What am I missing here?


Solution

  • Just a guess, because I've never used Padrino, but if it works like Rails this may help you:

    get '/testpage/:id' do
      @userID = params[:id]
      render 'test/index'
    end