Rails 7 #137 Use ViewComponent to Abstract reusable HTML. Blocks, Slots, Variables

preview_player
Показать описание
Don't repeat yourself (DRY) applies not only to Ruby code, but HTML and CSS too!

In this episode we will identify common HTML/CSS elements across some pages in our app and abstract it into a ViewComponent.

A View Component can accept variables and blocks of html to accomodate for the small differences of each of the pages.

View Components is an innovative approach for abstracting view logic elements in your Rails 7 frontend.

This might not be one of my "greatest" episodes, but I had to make it because it displays a great usecase for the right tool.

0:00 identify duplicate HTML/CSS
1:25 grenerate component and display variables
4:22 move duplicate HTML/CSS to ViewComponent
5:20 display content block in Component
6:20 optionally full width
8:35 reuse the Component in other parts of the app
10:11 ViewComponent Slots
12:57 Summary
Рекомендации по теме
Комментарии
Автор

Very Amazing Lecture, really Thank you

aman.asrani
Автор

Go on Yaro! Spreading ViewComponent love ♥

itsameandrea
Автор

Thanks for sharing. We can achieve same functionality using ActionView FormBuilder

nirav-kanani
Автор

Please make a video on Nested Attributes

rajamohsin
Автор

Thank you so much for the great material!
I would be grateful for the continuation of this topic.
More examples, complex, interlinked. Especially with the use of Turbo technology.
Слава Україні!

ledockol
Автор

This is awesome! Are you able to make a detailed video on deployment with MRSK and sidekiq?

theappsecguy
Автор

Hi Supe, thanks for the tutorial. I generated a component with a title attribute, and when I tried adding another attribute(body) to the initialize method it says 'ArgumentError missing keyword: :body'. Any ideas? I tried restarting the server but nothing changed.

def initialize(title:, body:)
@title = title
@body = body
end

shadowtman