Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 14, 2022 04:20 am GMT

HOTWIRE:TURBO

HOW TO IMPLEMENT HOTWIRE TURBO IN RUBY ON RAILS

Image description

To start implement hotwire turbo, first you need to install Redis server. This server need to be start alongside with the rails server in order for the apps to be able use Turbo Streams in your code.

Next, you have to configure your model by adding broadcast for the action that you want to use HOTWIRE:TURBO. eg:

`
class Comment < ApplicationRecord
has_many :likes, as: :likeable
belongs_to :user
delegate :user_name, to: :user

after_create_commit {broadcast_prepend_to "comment"}after_update_commit {broadcast_replace_to "comment"}after_destroy_commit {broadcast_remove_to "comment"}

end
`

When you done with that, select frame of data that you want to be replaced. and put turbo-frame tag and id in the tag to refer it from the other page and close it at the last data. Make sure both frame of data from different page have the correct id.

<%= turbo_frame_tag dom_id(comment) do %>
***** INSERT YOUR CODE *****
<%= link_to "Edit this comment", edit_comment_path(comment) %>
<% end %>

The URL link that refer to that frame must be contained inside the turbo-frame tag to successfully retrieved it.

If you do a form and want to be able to display the data that being output from the form, simply just enter turbo_stream_from tag followed by which page that you want to display.

<div class="container-fluid">
<% @comments.each do |comment| %>
<%= turbo_stream_from "new" %>
<%= turbo_frame_tag dom_id(comment) do %>
**** YOUR CODE ****
<% end %>
<% end %>
</div>

It will be displayed inside the turbo-frame tag. So, make sure the tag is already in the form page that you want to get the data.

Image description

FOR REFERENCE:
1) https://turbo.hotwired.dev/handbook/introduction
2) https://www.youtube.com/watch?v=Qp6sxgjA-xY&t=1026s
3) https://www.youtube.com/watch?v=eKY-QES1XQQ&t=2s

REDIS INSTALLATION:
1) https://www.youtube.com/watch?v=jkWtBN4Aw-I


Original Link: https://dev.to/megats/hotwireturbo-2mg2

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To