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
Ruby On Rails Session
Ruby on Rails is a powerful web application framework written in the Ruby programming language. It follows the convention over configuration principle, enabling developers to build applications quickly and efficiently. Rails emphasizes the use of RESTful design patterns and encourages the developmen
6 min read
Django vs Ruby On Rails
When you're building a website or web application, picking the right framework can be a big decision. Two popular choices are Django and Ruby on Rails. Django uses Python, while Ruby on Rails uses Ruby. Each has its own way of doing things, and they both have their strengths. This article will break
7 min read
Ruby on Rails Validation
Ruby on Rails (often just Rails) is a popular web application framework written in Ruby. One of its core features is validation, which ensures that data entered into a Rails application meets specific criteria before being saved to the database. Validations help maintain data integrity, enhance user
8 min read
Ruby on Rails Tutorial
Ruby on Rails or also known as rails is a server-side web application development framework that is written in the Ruby programming language, and it is developed by David Heinemeier Hansson under the MIT License. It supports MVC(model-view-controller) architecture that provides a default structure f
9 min read
Ruby on Rails - MVC
Ruby on Rails, also called Rails, is a web framework for server-side web applications that is implemented in Ruby. It was developed by David Heinemeier Hansson and launched in 2004. The philosophy is that application development should be easy, and it does so by making a set of guesses as to what ev
6 min read
Ruby on Rails - AJAX
AJAX (Asynchronous JavaScript and XML) is a web development technique used to create more dynamic and interactive web applications. In Ruby on Rails, AJAX is used to update parts of a web page without reloading the entire page. This is particularly useful for enhancing user experience by providing f
4 min read
Ruby on Rails - Scaffolding
Ruby on Rails (RoR) is an effective internet application framework that follows the version-View-Controller (MVC) structure. One of the standout features of Rails is its capacity to generate boilerplate code through scaffolding, which hastens the improvement system by presenting a foundation for CRU
5 min read
Ruby on Rails Filters
In the web development landscape, efficiently managing request-response cycles is paramount. Ruby on Rails, a prominent web application framework, provides a powerful feature called "filters" within its MVC architecture. Filters enable developers to execute specific code at defined points during the
2 min read
Ruby on Rails - Caching
Ruby on Rails provides a set of powerful caching mechanisms that can significantly help optimize the performance of web applications, which rely heavily on database operations and rendering. Caching in Rails means storing the result of expensive operations such as database lookups or complex view re
11 min read
Ruby | Hash values function
Hash#values() is a Hash class method which returns the values present in the hash. Syntax: Hash.values() Parameter: Hash values Return: hash values Example #1 : # Ruby code for Hash.values() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, b:200} # declaring
1 min read