News for hackers. Stuff that matters.
Re:Learn Stimulus in Ruby on Rails by Building a Toggle) by hbroadbent on Monday June 26, 2023 @06:52PM (#36486933)
Hey HN!

For those new to Rails, Stimulus (https://stimulus.hotwired.dev/) is a way to sprinkle frontend javascript code into your Rails views.

I held off using Stimulus in my Rails apps for a long time because I didn't understand it — the data= attributes looked weird, and the connection between my Rails views and Stimulus javascript controllers was confusing.

So I decided to write the guide I wish I had when I was learning Stimulus.

Hope you enjoy it! I'm happy to answer any questions, feel free to ask any.

Thanks, Harrison

Re:Learn Stimulus in Ruby on Rails by Building a Toggle) by marckohlbrugge on Monday June 26, 2023 @07:57PM (#36487445)
Great write-up!

I appreciate that you went the extra mile by showing how a generic enough controller can be used for various use cases.

People coming a from traditional JS background tend to make very specific Stimulus controllers, but when you make them a bit more abstract and modular they become more usable and easier to maintain.

Re:Learn Stimulus in Ruby on Rails by Building a Toggle) by hbroadbent on Monday June 26, 2023 @09:17PM (#36487900)
Thank you! I agree - I've seen other articles creating very specific controllers, which gets you into the issue of having heaps of specific controllers which could just be 1.

Glad you found the article useful!

Re:Learn Stimulus in Ruby on Rails by Building a Toggle) by mynonameaccount on Wednesday June 28, 2023 @12:26AM (#36502284)
It's not weird. It's just a different way of triggering javascript without using much javascript. Just use jQuery or pure javascript and stop having to learn new, not necessarily better, ways to do the same thing. Which is the main problem with Rails. They hide so much from the developer. Even hide the table id in the schema. It's as if a developer can't handle a extra line or something.
Re:Learn Stimulus in Ruby on Rails by Building a Toggle) by nickjj on Tuesday July 04, 2023 @01:23PM (#36589756)
> Even hide the table id in the schema. It's as if a developer can't handle a extra line or something.

That's because they can't handle it. I've seen a number of systems with 200-300+ tables that grew over 10+ years with 20+ developers working on it. This wasn't a Rails app btw, it's using a framework with much less opinions in a different language where an ORM wasn't being used.

You end up with `id` in some tables or `tablename_id` such as `post_id` as the primary key. It makes interacting with the app feel like a surprise depending on what section of the code base you're working on.

Re:Learn Stimulus in Ruby on Rails by Building a Toggle) by RileyJames on Monday June 26, 2023 @08:40PM (#36487717)
I love stimulus. And Hotwire. I find everything more maintainable and faster to developer when rails on the centre of the universe. Obviously a personal preference.

I faced a similar display toggle-able use case for form fields recently.

I did find that I used stimulus to hide the fields there was a slightly “flicker” as they appeared and then were hidden in the connect() function.

It didn’t affect my use case, as I could default hide and then toggle them to appear with stimulus.

But in the event they should appear by default and then be hidden based on some logic within the stimulus controller, is there a better way to handle this?

Re:Learn Stimulus in Ruby on Rails by Building a Toggle) by ignurant on Monday June 26, 2023 @10:34PM (#36488271)
I usually conditionally include the visibility toggle class from the erb side, so the first time it renders as html it has the toggle correctly initiated. I can see situations where the visibility logic is hard or impossible from the Ruby side, but I haven’t had this issue yet.
Re:Learn Stimulus in Ruby on Rails by Building a Toggle) by hbroadbent on Monday June 26, 2023 @11:12PM (#36488456)
Unfortunately I haven't come up with a good solution either beyond hiding the element with CSS, and then showing it when the controller connects (which isn't ideal). I'd love any suggestions though for different techniques
[dead]
Re:Learn Stimulus in Ruby on Rails by Building a Toggle) by 0xblinq on Wednesday June 28, 2023 @08:35AM (#36505483)
* * *