Ruby rails: views

From wikinotes
Revision as of 16:45, 19 November 2020 by Will (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Views are the actual HTML websites, formatted using templating logic.

They are preprocessed using ruby eruby, and have access to all @instance_vars on the controller.

You may also be interested in ruby rails: view helpers.


Documentation

Action/View docs https://guides.rubyonrails.org/action_view_overview.html
Action/View Form-helper docs https://guides.rubyonrails.org/form_helpers.html

Locations

{project}/app/views/{controller}/{method}.html.erb view files (preprocessed html files)

Share data with Layout

<!-- view.html.erb -->
<% content_for(:user_list) do %>  <!-- block saved as ':user_list' -->
  <ul>
  <% ["alex", "courtney", "sam"].each do |user| %>
    <li><%= user %></li>
  <% end %>
  </ul>
<% end %>
<!-- layout.html.erb -->
<html>
  <body>
    <%= yield :user_list %>  <!-- render 'user_list' defined above -->
    <%= yield %>             <!-- render view -->
  </body>
</html>