[Rails] View

Tags
Rails
Engineering
Created
Oct 18, 2023 03:19 AM
Edited
Oct 17, 2023
Description
Very basic Rails usage; erb

Basic example

  • Put this in list.html.erb
<% if @books.blank? %>
<p>There are not any books currently in the system.</p>
<% else %>
<p>These are the current books in our system</p>

<ul id = "books">
   <% @books.each do |c| %>
   <li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
   <% end %>
</ul>

<% end %>
<p><%= link_to "Add new Book", {:action => 'new' }%></p>
  • Mostly, you should create a html.erb for most of the actions, meaning that there will be new.html.erb, edit.html.erb, and etc.
    • Each html.erb is used by the action in the controller such as new, edit, index, and etc
  • Files are mostly inside app/views

erb

  • A Rails template
  • erb means “embedded ruby”
  • <% %> for Ruby code that does not return
  • <%= =%> for when you want output

Source