DEV Community

Kat
Kat

Posted on

Views and more

To incorporate Ruby and HTML together in a page, Sinatra provides a folder called views, where you can put .erb files. These files are primarily HTML but you can add erb tags to inject some Ruby, which your browser will read as the HTML the Ruby code snippets represent. This is a great way to put logic or variables directly into your HTML templates.

If you have code that will be repeated in various templates, this can be placed in the folder layout.erb. The code from the other erb files will appear where you place the <%= yield %> tag.

Putting { :layout => false } in the app.rb file for a specific route will turn off the layout for that particular view, or you can override the layout with a different one using { :layout => :other_layout }.

use :variable in your path to create a dynamic variable. The variable params allows us to get those in the form of a hash.

We also learned about the importance of automated tests. They're super helpful when refactoring code to make sure that the functionality of the original has been maintained, and can test a number of user inputs automatically. While writing the code for these tests takes some time, possibly longer than writing the code that will be tested itself, it is a great time investment as it ensures functional code over time and over changes to the codebase.

Top comments (0)