Skip to content
How to access backend vars on the frontend in Ruby
Profile icon
JacobMcPherson1

How would I access the visitor's var in my index.html file?

You are viewing a single comment. View All
Profile icon
CodingCactus

you should use erb files, not html ones

A little example:

main.rb

get "/" do @randnum = rand(1..10) erb :index end

public/index.erb

<p><%= @randnum %></p>

If you need some explanation, let me know!

Profile icon
JacobMcPherson1

@CodingCactus It works fine, but can you explain what an erb file is? Thank you!

Profile icon
CodingCactus

@JacobMcPherson1 you may want to google it as I'm not very good at explaining it, but basically it's like ejs but ruby

Profile icon
JacobMcPherson1

@CodingCactus oooh, ok. Thanks again