2 m read

Views in Rails

Welcome back! Having discussed controllers in our previous article, we’re now moving on to the V in MVC – Views. In Ruby on Rails, a View essentially operates as an ERb (Embedded Ruby) program that shares data with controllers through mutually accessible variables.

Let’s concentrate on understanding how views function in Rails and how they can effectively present data in a simplified format.

Fundamentals of Action View in Rails

To start, let’s revisit Action View, which is a package in Rails that allows you to craft beautiful and tailored user interfaces. It facilitates the writing of templates using a mixture of Ruby and HTML and is not dependent on Active Record, the model part of Rails MVC.

How Templates Work in Rails

An Action View template serves as the core logic of your view. These templates offer flexibility by allowing various writing methods. If the file has a .erb extension, it employs a combination of ERB and HTML. Alternatively, the Builder::XmlMarkup library can be used to generate XML content. Builder templates are especially useful for generating XML content.

Reusable Parts: Partials and Layouts

To optimize your code and avoid duplication, “partials” are a perfect solution. Partials consist of code fragments that can be reused in various areas of your application. When creating a partial view file, convention dictates naming it with a leading underscore (_). The “render” method is then used to include that partial in a view.

A common view template can encompass the outcomes of a controller action using a “layout”. Similar to Partials, a layout can also incorporate its own partials to ensure a consistent look and feel throughout the application.

Localizing Views and Utilizing I18n

Action View also allows you to render different templates based on the current locale. This is incredibly useful when building applications with multi-language support. You can leverage Rails’ I18n system to display content that differs depending on the user’s locale or any other factors you need to consider.

Working with Rails Views in Practice

Rails Views and Controllers

In practice, every method you define in the controller needs an associated .erb file, bearing the same name as the method, to display the data collected. The views files correspond directly to the methods we have in the controllers.

Creating and Manipulating Views

When creating a new book in our book management application, for example, a form is rendered to the user using Ruby embedded within HTML.

To create this form, you would create a new file, new.html.erb, in the /app/views/book directory and start coding the form. Upon submission, data can be saved to the database.

Working with Associated Objects in Views

The beauty of Rails Views is that they can take full advantage of the associations built within your models. For example, pulling the subject’s name value through the @book variable using belongs_to associations requires the call “@book.subject.name”. As you can see, there is much power in this simplicity.

Deleting Data with Views and Controllers

Deleting data in Rails is quite straightforward. There’s no need to write any view code for the delete method because the method simply redirects to a ‘list’ method to display the result. Thus, you can easily manage records using Rails Views.

Conclusion

Understanding Rails Views and how they interact with controllers is an essential part of mastering the Ruby on Rails framework. This includes understanding templates, layouts, partials, and localized views.

Now, equipped with this knowledge, you are ready to make your web applications more interactive and user-friendly. See you in the next article!

Continue your study of Ruby on Rails by reading our next article.

Benji

Leave a Reply