Saturday, June 7, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

Building reusable UI components in Rails with ViewComponent

February 7, 2024
in Front-Tech
Reading Time: 4 mins read
0 0
A A
0
Share on FacebookShare on Twitter



Reusable UI components are HTML tags that can be used in different parts of a web project. These components are usually small and designed for specific functionalities. Instead of coding everything from scratch, you can write the code once and then import these UI components wherever needed. For instance, you can create a card component to display information or a navigation bar that appears at the top of all web pages. These UI elements can be imported and added to any desired web page. Other examples of reusable UI components include input fields, buttons, modals, and progress bars. There are several advantages to using reusable UI components. They save time and effort by allowing you to reuse code instead of starting from scratch. They improve code quality and maintainability, bringing consistency to your project. Additionally, reusable components make your project more modular, making it easier to track down and isolate errors and bugs. In this tutorial, we will guide you on how to identify reusable components in your project and how to develop these UI widgets in Rails. Let’s get started.

Identifying and extracting reusable components
It is good practice to analyze the structure of your application and identify parts that can be broken down into smaller, reusable components. Look for common functional patterns that appear in multiple places in your application. Pay attention to the relationship between models, controllers, and views to identify areas where they share logic or functionality. Also, inspect your application for instances of code repetition, as this may indicate the need for a reusable component. Review your view templates and files to identify common HTML elements, forms, and other components that can be modularized.

Creating reusable components
In this step, we will learn how to create reusable components in Rails using ViewComponent. ViewComponent is a Ruby on Rails library that helps create reusable and encapsulated UI components for your application. By breaking down an application into smaller chunks, this framework makes it easy to perform unit tests and track down sources of error, facilitating faster debugging.

We will create a simple website using reusable components. Specifically, we will create two reusable UI components: the navigation bar and the product card.

Getting started
Add the following code to your Gemfile and run the bundle install command to install the ViewComponent library into your project.

“`html
$ bundle install
Fetching gem metadata from https://rubygems.org/
Resolving dependencies…
Using rake 13.0.6
Using concurrent-ruby 1.2.2
Fetching view_component 3.6.0
Using rails 7.0.8
Installing view_component 3.6.0
Bundle complete! 15 Gemfile dependencies, 76 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
“`

Creating components
Components in ViewComponent are subclasses of ViewComponent::Base. When you create a ViewComponent, an app/components directory is generated to store all the component files. It is best practice to name components based on what they do or render, rather than what they accept. For example, a CardComponent is preferred over a ReportComponent. Components can contain HTML, CSS, and JavaScript code. You can also add logic to guide the behavior of these components in a separate Ruby class.

To create a ViewComponent, run the following code, replacing “Navigation” with the desired name of your component:

“`html
bin/rails generate component Navigation nav
“`

This will generate a navigation_component file to store your component’s logic, and a navigation_component.html.erb file for your HTML and CSS code.

The navigation_component.rb file should look like this:

“`html
# frozen_string_literal: true

class NavigationComponent < ViewComponent::Base def initialize(nav:) @nav = nav end end ``` The navigation_component.html.erb file should look like this: ```html

Add Navigation template here

“`

You can add navigation elements to the template, as shown below. We’re using TailwindCSS for styling, but you can also use custom stylesheets.

“`html

  • Home
  • Phones
  • TVs

“`

To render the component, add the following code snippet to the respective view. In this case, we will render the element in the `application.html.erb` layout file since we want it displayed across all web pages. Use the `<%= render NavigationComponent.new(nav: @nav) %>` code to render the nav component in the browser.

“`html



Shopit

<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "tailwind" , "inter-font", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>



<%= render NavBarComponent.new() %>
<%= yield %>



“`

Let’s create a new CardComponent to display items retrieved from the database. To generate the component, run the following command:

“`html
bin/rails generate component ItemCardComponent title description
“`

Replace “title” and “description” with the desired variables for your product.

The item_card_component.rb file should look like this:

“`html
# frozen_string_literal: true

class ItemCardComponent < ViewComponent::Base def initialize(title:, description:) @title = title @description = description end end ``` The item_card_component.html.erb file should look like this: ```html

<%= @title %>

<%= @description %>

“`

You can also create a BannerComponent to communicate information to users. Run the following command to generate the component:

“`html
bin/rails generate component banner title message
“`

Replace “title” and “message” with the desired variables for your banner.

The banner_component.rb file should look like this:

“`html
# frozen_string_literal: true

class BannerComponent < ViewComponent::Base def initialize(title:, message:) @title = title @message = message end end ``` The banner_component.html.erb file should look like this: ```html

<%= @title %>

<%= @message %>

“`

To render the ItemCardComponent and BannerComponent in the index.html.erb file, use the render() method as shown below:

“`html

Dashboard

<% @products.each do |product| %>
<%= render(ItemCardComponent.new(title: product.title, description: product.description)) %>
<% end %>

<%= render(BannerComponent.new(title: "Status", message: "Completed")) %>

“`

You can download the full code used in this tutorial from this GitHub repository.

Best practices for building reusable UI components
When creating reusable UI components, it is important to follow certain principles. These practices will help you create components that are easier to understand, configure, and manage.

1. Ensure each component has a specific responsibility: Each component should have a clear purpose, making it easier to understand its usage and how it fits into a project.

2. Use props to pass values to UI: Reusable components should be flexible and dynamic. They should accept different types of data, such as collections, strings, and objects, to



Source link

Tags: BuildingComponentsRailsreusableViewComponent
Previous Post

National Australia Bank names Andrew Irvine as new CEO By Reuters

Next Post

How To Create A JavaScript Countdown Timer For Beginners

Related Posts

The essential principles of a good homepage
Front-Tech

The essential principles of a good homepage

June 7, 2024
How to measure and improve user retention
Front-Tech

How to measure and improve user retention

June 6, 2024
Push Animation on Grid Items
Front-Tech

Push Animation on Grid Items

June 5, 2024
How to build a Rails API with rate limiting
Front-Tech

How to build a Rails API with rate limiting

June 4, 2024
Introduction to the B.I.A.S. framework
Front-Tech

Introduction to the B.I.A.S. framework

June 3, 2024
Blue Ridge Ruby is exactly what we need
Front-Tech

Blue Ridge Ruby is exactly what we need

June 3, 2024
Next Post
How To Create A JavaScript Countdown Timer For Beginners

How To Create A JavaScript Countdown Timer For Beginners

Nine Rules for Accessing Cloud Files from Your Rust Code | by Carl M. Kadie | Feb, 2024

Nine Rules for Accessing Cloud Files from Your Rust Code | by Carl M. Kadie | Feb, 2024

Biden wins Nevada Democratic primary, NBC News projects

Biden wins Nevada Democratic primary, NBC News projects

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
23 Plagiarism Facts and Statistics to Analyze Latest Trends

23 Plagiarism Facts and Statistics to Analyze Latest Trends

June 4, 2024
Managing PDFs in Node.js with pdf-lib

Managing PDFs in Node.js with pdf-lib

November 16, 2023
Accenture creates a regulatory document authoring solution using AWS generative AI services

Accenture creates a regulatory document authoring solution using AWS generative AI services

February 6, 2024
Azul cloud service spots dead code in Java apps

Azul cloud service spots dead code in Java apps

October 7, 2023
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
Turkish Airlines Marketing Strategy: Beyond “Globally Yours”

Turkish Airlines Marketing Strategy: Beyond “Globally Yours”

May 29, 2024
Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

June 10, 2024
AI Compared: Which Assistant Is the Best?

AI Compared: Which Assistant Is the Best?

June 10, 2024
How insurance companies can use synthetic data to fight bias

How insurance companies can use synthetic data to fight bias

June 10, 2024
5 SLA metrics you should be monitoring

5 SLA metrics you should be monitoring

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

June 10, 2024
Facebook Twitter LinkedIn Pinterest RSS
News PouroverAI

The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

CATEGORIES

  • AI Technology
  • Automation
  • Blockchain
  • Business
  • Cloud & Programming
  • Data Science & ML
  • Digital Marketing
  • Front-Tech
  • Uncategorized

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 PouroverAI News.
PouroverAI News

No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing

Copyright © 2023 PouroverAI News.
PouroverAI News

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In