Problems Integrating Hotwire Into Existing Rails Project

Hello everyone. I just joined this community and this is my first post.

I’ve been following several Hotwire tutorials and have built some prototypes with the technology. I’ve been impressed with how easy it is to get SPA-like responsiveness with much less complexity compared to JS front end frameworks.

However, I’ve found that when I try to translate the code and process from my prototypes to my existing Rails project it simply never works. What’s worse is that I don’t know why.

I’ve tried to write some tracer code with the following general process:

  1. Include the Hotwire gem
  2. Install Hotwire via terminal command
  3. Include broadcasts from the relevant model
  4. Apply a turbo_stream_from tag as a listener at the view level
  5. Include a turbo_frame_tag where you want the model data to be updated

This works reliably in my prototypes, but won’t work in my existing project. I’m suspecting it may be because my existing project isn’t using the latest version of Rails nor Ruby? (Upgrading has not proved easy either.) I am currently using Ruby version 2.7.1 and Rails version 6.0.2.

If it helps at all these are the kinds of terminal messages I’m getting when attempting to get Hotwire to autoupdate a list of model instances on an index page. It seems like the broadcast is working but my listener isn’t?

If anyone would be able to offer me some guidance I’d REALLY appreciate it. Thank you!

Hey, that console log seems pretty normal to me. Do you have an element with an ID of stories on the page? Turbo wants to append your stories to that.

Here’s how that might look:

<div id="stories">
  <%= render @stories %>

If that’s not the issue, it might be something to do with your job queue? :thinking:

dan, thanks so much for your reply.

Indeed, it looks like I’ve got the turbo frames and HTML elements setup correctly. I’ve attached a screenshot of what the HTML source looks like via chrome dev tools. The story model instances are appending properly, but they update ONLY when I refresh the page. For whatever reason the turbo frame tag is not updating automatically though.

Argh! It’s so frustrating.

As for the job queue aspect, I will do my best to investigate that. Would you be able to point me to a relevant debugging source?

I wanted to circle back to this post to report that I didn’t find a solution. This is despite spending over a week’s worth of full-time hours trying to debug this problem.

In the end it was just faster and easier to start a new repo with the latest version of Ruby, Rails, and all of the gems. I proceeded to simply copy and paste as much code as I could into the new repo. Beyond that, I retraced my steps with my terminal commands and basically created a clone of the original app.

Lo and behold, it works.

The new app uses Ruby version 3.0.1 and Rails version The old iteration of the app (the one that isn’t working) runs on Ruby version 2.7.1 and Rails version

Thank you to everyone who reached out to try and help me in this matter.

1 Like