Ask coding questions

← Back to all posts
How to access backend vars on the frontend in Ruby
JacobMcPherson1 (239)

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

Answered by CodingCactus (4366) [earned 5 cycles]
View Answer
Comments
hotnewtop
CodingCactus (4366)

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!

JacobMcPherson1 (239)

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

CodingCactus (4366)

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