top of page

I was wrong about turbo in models



Thinking about Rails and Turbo

In videos and posts, I express my opinion about using stimulus_reflex, turbo_streams, and turbo_frames.

I explained that I use the Model, not controllers, to dispatch actions to turbo_streams.

But talking with Frank, that is one of the best developers that I know, said a few things that I wasn't thinking right: the basic idea of Ruby on Rails is MVC, so the lower part of the framework doesn't have the responsibility to change the top part of the framework like Model changing the View. And this is right: The Model just does what it is designed to do. To have some interaction with View, the Controller should dispatch the modifications, like turbo_stream changing places when some action occurs.

But in the video that I watched, the idea of Model Streams, something to view, is still happening. And now, I can give my opinion: I don't do that in my codes.

Why? Let's see this situation.

  • You have a view post action in the front end and a list of comments.

  • The user can add comments and delete what he created.

  • When creating a comment occurs, the application should update the home page on the top with the last comment; in other actions, the number of comments and the last one what was the comment date.

  • Until now, it's ok, but if the content should update just 2 places, the application will not dispatch the stream when the action occurs because nothing that involves the operation of the update affects it.

  • The action of the update will dispatch streams without motive.

  • Consuming the lower to the high part of MVC without motive

And now I still using stimulus_reflex. This gem can dispatch events to reflex classes#actions, and there, you can save, update, and delete the instance. You can also edit the application with streams, 5 dispatches of the stream when creating the comment, and 3 when updating from the same local. But imagine another form with a comment or link to update the comment and dispatch 2 streams, not 3. My solution is to use stimulus_reflex with a class and different actions. In each action, dispatch what is needed. With this, the Model is still with your purpose, and the same operation as the updated Model will dispatch just what needs to be updated for different actions.

Comments


Captura de tela de 2024-01-01 22-06-25.png

Hi, I'm Rodrigo Toledo

A full-stack developer skilled in both front-end and back-end development, building and maintaining web applications

  • Facebook
  • Youtube
  • LinkedIn
  • Instagram

I don't know everything, help me

Every day, I try to improve what I know about frameworks, and when this occurs, I'll try to share it with the community. Every day, I try to improve my knowledge about frameworks. When this happens, I will try to share it with the community.

Subscribe

Thanks for submitting!

bottom of page