ERB (with Rails)

Intro to ERB

  • “Embedded Ruby”
  • One of a class of ‘template languages’
  • There are many
  • They do a simple job (used to be called “mail merge”)
What it does
  • Erb file without any `` blocks remains unchanged
  • `` must contain legal ruby code
  • Let’s look at ErbDemo
  • The code between the angle brackets is evaluated.
  • *And only in the case of `` it’s inserted into the resulting text

How it works with Rails

  • ERBs are used primarily in views (also known as view templates in rails)
  • Rails has numerous “helper” methods that work very nicely with erb
  • In “routes.rb”: resource or get or path or other command “automatically” defines one or more helpers.
  • In the example below, “sessions_path” was generated by the resources :sessions line in routes.rb
  • ** Reference:** Rails Form Helpers
Example
  <% form_for(:sessions, url: sessions_path) do |f|  %>
      <div>
        email: <%= f.text_field :email %>
      </div>
      <div>
        password: <%= f.password_field :password %>
      </div>
      <%= f.submit "Sign in" %>
  <% end %>