Hacker News new | ask | show | jobs
by mike1o1 2030 days ago
Stimulus has a very small surface area, but I find it really enjoyable to use when combined with Turbolinks.

My only complaint/comment is that some of the HTML that I need to generate ends up being littered with so many data attributes, especially when combining multiple Stimulus controllers/behaviors.

Has anybody found a clean way around that in Rails templates?

3 comments

Same advice as when using Tailwind CSS, or anything of this sort: refactor your application-specific front-end parts at the point of generation, into purposefully named units. For Rails, that means shifting your boilerplate into helpers, partials, and partial layouts. If you're ready for a sharper leading edge, look at ViewComponent.

Once the boundaries of your server-side parts for something are congruent to the client-side parts, you have reached the nirvana state of wondering, "can I extract & package these into a library and share it?".

This is general advice for all development but particularly and specifically true for Rails devs.

I've found it works well with [ViewComponent](https://github.com/github/view_component) if you're in to that.
I share some of the same issues, I have found that ERB templates work better than HAML for lots of data attributes.

Basecamp seems to use liberal newlines / spacing when creating html tags (like one line per attribute) so that might help.

Server-side "component partials" still don't have a great story, but view_component is one emerging option.

As another option, HAML lets you extract the attributes into a ruby helper method:

https://haml.info/docs/yardoc/file.REFERENCE.html#attribute-...

Same. I’ve been a happy user of Haml for about ten years, but I think that Tailwind and Stimulus will permanently force me back to ERB :(