Rendering an HTML form without using ERB

Viewed 178

In an old thesis, I found a ruby method that looks like this:

get "/test" do

  # Render an html form 
  <<-HTML
  <html>
     <!--just some code here-->
  </html>
  HTML
end

The method directly renders an html form without making any calls to an erb file. Is this typically a valid way to do it? I haven't found a single tutorial online that demonstrates it like this. Every source that I've looked at talks about erb.

2 Answers

ERB is HTML with Ruby code sprinkled in so you can do something like

  <tbody>
      <% @books.each do |book| %>
        <tr>
          <td><%= book.title %></td>
          <td><%= book.content %></td>
          <td><%= link_to "Show", book %></td>
          <td><%= link_to "Edit", edit_book_path(book) %></td>
          <td><%= link_to "Destroy", book, method: :delete, data: { confirm: "Are you sure?" } %></td>
        </tr>
      <% end %>
    </tbody>

You can use html if no ruby logic is to be added in the views..

The excerpt looks like part of a Sinatra-based app (a lightweight ruby web framework, official site).

ERB is ruby-based template language, it is quite popular due to it's simplicity and speed, but when you do not need large responses or logic in rendering and only want to output some string - it's pretty valid to do just it. No code runs faster than no code.

Related