zhephyn Profile picture
Oct 14 11 tweets 3 min read Read on X
Your first Hotwire application, a blog series. Part 2
This part of the series will serve as a gentle introduction to what Turbo frames are used for and how to integrate them into the blog application we built in the first part of the series. x.com/zhephyn/status… x.com/zhephyn/status…
We use Turbo frames to "slice" a web page into independent sections, and have updates applied to these individual sections instead of the entire page.
Enclosing a section of a page in a turbo frame tag like below, turns that section into a Turbo frame. Image
In the above code, we wrapped the statement in the p tag in a turbo frame do-end block thus turning it into a turbo frame. The phrase "paragraph turbo frame" is the id of the newly created turbo frame and is important for identification purposes.
For this application, we'll use Turbo frames to restrict most CRUD actions to happen on the index page. We'll use frames to render the form to create a new post on the index page, apply inline editing as well as for displaying longer versions of posts. We'll proceed to do these.
1. Rendering a "create post" form on the index page.
With a combination of the turbo frame tag and the dom_id helper, we'll be able to generate "id's" for the Turbo frames crucial for this implementation. Since this is a model-backed resource, typing out string id's for the...
various turbo frames, like in the example code above wouldn't be viable.
- Create an empty turbo frame on the index page like below: Image
- Wrap the code associated with displaying the "create post" form in a turbo frame do-end block with an id similar to that of the empty turbo frame on the index page. Image
- Edit the "New Post" button on the index page by adding a data-turbo-frame attribute to it. With this change, clicking this button will trigger the "swapping" mechanism by which Turbo frames operate namely, the empty turbo frame on the index page will be "replaced" by the....
turbo frame enclosing the "create post" form. To achieve this, we'll pass in an id for to the data-turbo-frame attribute, similar to that of the 2 associated Turbo frames like below; Image
On trying it out in the browser, displaying the form on the index page as well as creation of the new post works as expected. However, we get a content missing error and the newly crated post isn't added seamlessly to the index page. We'll solve this in a few.
2. Inline Editing for posts
- Wrap the edit form in a Turbo frame tag Image

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with zhephyn

zhephyn Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @zhephyn

Aug 21
This tutorial is meant to serve as an introduction to Action Cable. For those unfarmiliar with what Action Cable is, basically all things real-time in rails are handled by action cable. I'll take you from the basics of action cable configuration up to the final version of the app
Below is a gif of what we'll build. A simple app where by we navigate to a room in the browser and we see messages appended to that room in real-time without the need for refreshing.
1. Create a new rails app of your desired name. I've chosen action_cable_chat as the name of my app.
2. Navigate to the folder where you've created your new rails app and cd into it
Read 18 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(