I’m pretty sure this may have been answered 5 or 10 years ago but given it’s 2024 and Rails 7.1 Hotwire era I would like to hear your opinion. So please hear me out:
Recently I’ve tried component based libraries like ViewComponent, Phlex and I see why devs like them = the code logic is close to HTML = less of a mess when complex view logic is required (same reasons why React Components are loved) .
But to be honest the more I’ve implemented these component gems in my personal projects less “Rails-like” those applications felt = I was over-abstracting most of ordinary things for no good reason.
So I’ve circled back to way how I would write views 15y ago where I would mix code logic and HTML directly in ERB view/partial. Here is an example what I mean:
<!-- app/views/comments/_comment.html.erb -->
<%
avatar = comment.user.avatar.variant(resize_to_fill: [300, 300])
something_complex_to_calc = comment.user.articles.where(whatever: "complex").order(id: :desc).count
user_name = comment.user.want_to_stay_annonymus? ? "Another User" : comment.user.name
%>
<%= turbo_frame_tag comment do %>
<div class="p-3 flex justify-between">
<%= image_tag avatar, class: "mr-2" %>
<div>
<p class="pb-2">
<%= truncate comment.body, length: 255 %>
<small class="font-semibold text-gray-500"><%= user_name %> (<%= something_complex_to_calc %>)</small> - <%= time_ago_in_words(comment.created_at) %>
</div>
<div>
<% end %>
I feel everything components have to offer from maintainability perspective can be achieved with Rails views/partials . I’m writing every personal project this way and I’m impressed how obviously productive and maintainable this is. (Given I try to keep small CRUD controllers supported by Hotwire)
My question is: is this a good idea? I’m worried about long term performance.
But Given I have good Russian doll caching practices I believe this shouldn’t be an issue
But even if they are not cached is this big deal in 2024 ? (Ruby 3.3 fast, Rails fast
)
What do you think? Any better ideas? Thank you
@DHH if you find the time, I would love to hear your opinion too pretty please ![]()
P.S. And yes there are always ViewObjects, Presenters, FormObjects,… but my opinion is those should be reserved for “there’s no other way out” situations. I’m more concerned about 95% of app views