Data Presentation is the responsibility of the views in Ruby on Rails. They are templates that combine HTML with embedded Ruby code (ERB) for the purpose of generating content dynamically. In this article, we will look into creation of view files for different methods in a Rails controller, including list, new, show, edit, delete and show_subjects.
Creating View File for 'list' Method
The 'list' method in Ruby on Rails is used to display a collection of records, such as a list of all subjects or posts.
Example:
Ruby
# app/views/subjects/list.html.erb
<h1>All Subjects</h1>
<table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
<% @subjects.each do |subject| %>
<tr>
<td><%= subject.title %></td>
<td><%= subject.description %></td>
</tr>
<% end %>
</table>
Output
List viewExplanation:
Looping through the @subjects collection provided by the controller, the above code produces a table which enumerates all subjects. In each row of the table displayed is a title and description that belongs to the corresponding subject.
Creating View File for 'new' Method
The 'new' method is used to render a form for creating a new record.
Example:
Ruby
# app/views/subjects/new.html.erb
<h1>New Subject</h1>
<%= form_with(model: @subject, local: true) do |form| %>
<div>
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :description %>
<%= form.text_area :description %>
</div>
<div>
<%= form.submit "Create Subject" %>
</div>
<% end %>
Output
new viewExplanation:
The form_with helper method creates a form that sends data to the correct controller action. Its fields are for title and description respectively matching with @subject model's attributes.
Creating View File for 'show' Method
The 'show' method displays details of a specific record.
Example:
Ruby
# app/views/subjects/show.html.erb
<h1><%= @subject.title %></h1>
<p><%= @subject.description %></p>
Output
show viewExplanation:
The view shows the title and description of @subject object which has been passed by the controller action. This is often used for viewing details of one record.
Creating View File for 'edit' Method
The 'edit' method provides a form for editing an existing record.
Example:
Ruby
# app/views/subjects/edit.html.erb
<h1>Edit Subject</h1>
<%= form_with(model: @subject, local: true) do |form| %>
<div>
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label : description %>
<%= form.text_area :description %>
</div>
<div>
<%= form.submit "Update Subject" %>
</div>
<% end %>
Output
edit viewExplanation
Like in case of new view, this one allows users to change current values of subject's attributes. Fields in the form are already filled in with data belonging to @subject.
Creating View File for 'delete' Method
The 'delete' method provides a confirmation before deleting a record.
Example
Ruby
# app/views/subjects/delete.html.erb
<h1>Delete Subject</h1>
<p> Are you sure you want to delete <%= @subject.title %>?</p>
<%= button_to "Delete", subject_path(@subject), method: :delete, data: { confirm: "Are you sure?" } %>
<%= link_to "Cancel", subjects_path %>
Output
delete viewExplanation
The button_to helper creates a form that has a button used to delete subject. Also, link_to helps us make a cancellation link to stop deletion and go back to subject's list.
Creating View File for 'show_subjects' Method
The 'show_subjects' method might display a list of subjects related to a particular record, like showing subjects under a category.
Example
Ruby
# app/views/subjects/show_subjects.html.erb
<h1>Subjects for <%= @category.name %></h1>
<ul>
<% @category.subjects.each do |subject| %>
<li><%= subject.title %></li>
<% end %>
</ul>
Output
show_subjects viewExplanation
This view file represents subjects from particular category as provided by @category object, all subjects are listed as items of unordered list.
Conclusion
For HTML content to be generated dynamically, it is important to create views in Ruby on Rails. By using ERB, you can simply insert a Ruby code into your templates, which makes it easy for displaying and manipulating data within web applications.