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.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read