39
loading...
This website collects cookies to deliver better user experience
Turbo: It is the heart of Hotwire and it is kind of a new version of turbolinks. Also, it is used for speed and broadcasts updates and it has two elements: frames and streams.
Stimulus: It is used for flexibility and it is similar to JavaScript framework but for html — it does not deal with rendering HTML at all. In addition, Stimulus pairs perfectly with Turbo to provide quick fixes with minimal effort.
Strada: It is not released yet; it will be used for mobile hybrid applications.
bundle install
rails hotwire:install
yarn install
turbolinks
from your app/javascript/packs/application.js
.app/views/layouts/application.html.erb
change the lines related to data-turbolinks-track
to data-turbo-track
.Start by setting up Turbo feature frames. This provides built-in updates to parts of the page and not to the entire page.
Add ActionCable broadcast channel preferences to your model.
# app/models/tags.rb
class Tag < ApplicationRecord
belongs_to :tag_type
validates :name, presence: true, uniqueness: true
after_create_commit { broadcast_prepend_to "tags" }
after_update_commit { broadcast_replace_to "tags" }
after_destroy_commit { broadcast_remove_to "tags" }
end
theturbo_stream_from
, which tells Turbo where we get the updates from. It should be the same definition we used in our model for this example "tags".
# app/views/tags/_tag.html.erb
<%= turbo_frame_tag dom_id(tag) do %>
<i class="tiny material-icons">local_offer </i> #{tag.name}
<% end %>
turbo_frame
to mark which parts we want to update in our app.
# app/views/tags/index.html.erb
<h5 class="center-align">Tags</h5>
<%= turbo_stream_from "tags" %>
<%= turbo_frame_tag "tags" do %>
<%= render @tags %>
<% end %>
<%= turbo_frame_tag "tag_form" do %>
<%= render "form", tag: @tag %>
<% end %>