<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jennifer Ledezma </title>
    <description>The latest articles on Forem by Jennifer Ledezma  (@jennlva).</description>
    <link>https://forem.com/jennlva</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F657408%2F77e39713-3c74-4f98-9559-46875750f87b.jpeg</url>
      <title>Forem: Jennifer Ledezma </title>
      <link>https://forem.com/jennlva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jennlva"/>
    <language>en</language>
    <item>
      <title>Modern Web Applications with Hotwire</title>
      <dc:creator>Jennifer Ledezma </dc:creator>
      <pubDate>Mon, 12 Jul 2021 15:46:24 +0000</pubDate>
      <link>https://forem.com/jennlva/modern-web-applications-with-hotwire-36lj</link>
      <guid>https://forem.com/jennlva/modern-web-applications-with-hotwire-36lj</guid>
      <description>&lt;h1&gt;
  
  
  Why use Hotwire?
&lt;/h1&gt;

&lt;p&gt;Rails views can sometimes be fast and work perfectly, but some other times they can cause all kinds of problems. For example, one of the most common situations is rendering a lot of partials that can make the application look slow. That could happen especially when the programmer is not careful about anti-patterns associated with Rails views.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0eby7scw9hqpgwwxnxto.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0eby7scw9hqpgwwxnxto.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, JavaScript has been used as an alternative to dynamically update our content without having to refresh the entire browser. However, it is not a solution loved by many programmers: "Why would I want to leave my Ruby and Rails bubble of happiness?" (R, Subramaniamso, 2021).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is why&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hotwire uses JavaScript libraries that provide connections to develop a more interactive and modern response system without the need to write a lot of JS code. And, the most important thing: without sacrificing any of the speed or responsiveness associated with a traditional single-page application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hotwire has 3 Components
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Strada: It is not released yet; it will be used for mobile hybrid applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Moreover, Turbo uses redis to store its data and handle websockets with action cable. This functionality is needed to autoloading the stimulus controllers without having to change anything in your controller classes to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set-up
&lt;/h2&gt;

&lt;p&gt;Add the hotwire-rails gem in your Gemfile file. This gem includes setup for Turbo and Stimulus; it also includes Redis. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run &lt;code&gt;bundle install&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;rails hotwire:install&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;yarn install&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Those steps will remove everything related to &lt;code&gt;turbolinks&lt;/code&gt; from your &lt;code&gt;app/javascript/packs/application.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Manually in your &lt;code&gt;app/views/layouts/application.html.erb&lt;/code&gt; change the lines related to &lt;code&gt;data-turbolinks-track&lt;/code&gt; to &lt;code&gt;data-turbo-track&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to use Hotwire&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Start by setting up Turbo feature frames. This provides built-in updates to parts of the page and not to the entire page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add ActionCable broadcast channel preferences to your model.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/models/tags.rb&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tag&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:tag_type&lt;/span&gt;
  &lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;presence: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;uniqueness: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;

  &lt;span class="n"&gt;after_create_commit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;broadcast_prepend_to&lt;/span&gt; &lt;span class="s2"&gt;"tags"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;after_update_commit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;broadcast_replace_to&lt;/span&gt; &lt;span class="s2"&gt;"tags"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;after_destroy_commit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;broadcast_remove_to&lt;/span&gt; &lt;span class="s2"&gt;"tags"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Edit your view, add &lt;code&gt;theturbo_stream_from&lt;/code&gt;, which tells Turbo where we get the updates from. It should be the same definition we used in our model for this example "tags".
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/views/tags/_tag.html.erb&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= turbo_frame_tag dom_id(tag) do %&amp;gt;
 &amp;lt;i class=&lt;/span&gt;&lt;span class="s2"&gt;"tiny material-icons"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;local_offer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/i&amp;gt; &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sr"&gt; 
&amp;lt;% end %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create our &lt;code&gt;turbo_frame&lt;/code&gt; to mark which parts we want to update in our app.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/views/tags/index.html.erb&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h5&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"center-align"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="no"&gt;Tags&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h5&amp;gt;
&amp;lt;%= turbo_stream_from "tags" %&amp;gt;
&amp;lt;%= turbo_frame_tag "tags" do %&amp;gt;
 &amp;lt;%= render @tags  %&amp;gt;
&amp;lt;% end %&amp;gt;
&amp;lt;%= turbo_frame_tag "tag_form" do %&amp;gt;
  &amp;lt;%= render "form", tag: @tag   %&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Wrapping up
&lt;/h1&gt;

&lt;p&gt;The combination of Hotwire and Rails with a pinch of Stimulus for client-side interactivity, it is a powerful option for building high-performance, scalable, and easy-to-use web applications for both developers and users. Overall, you just have to consider using a design that fits this excellent solution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Few7gbravdzkufff820q3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Few7gbravdzkufff820q3.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blog.magmalabs.io/2021/07/08/hotwire-rails.html" rel="noopener noreferrer"&gt;See the original post on MagmaLabs Technical Blog:&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>hotwirerails</category>
      <category>stimulus</category>
    </item>
  </channel>
</rss>
