<?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>DEV Community: Jude</title>
    <description>The latest articles on DEV Community by Jude (@judelawrence).</description>
    <link>https://dev.to/judelawrence</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%2F892348%2Fac3c1b23-18d6-4304-95a5-683679d9a1d3.jpeg</url>
      <title>DEV Community: Jude</title>
      <link>https://dev.to/judelawrence</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/judelawrence"/>
    <language>en</language>
    <item>
      <title>Turn a checkbox into a button without any javascript.</title>
      <dc:creator>Jude</dc:creator>
      <pubDate>Sun, 02 Jul 2023 05:44:45 +0000</pubDate>
      <link>https://dev.to/judelawrence/turn-a-checkbox-into-a-button-without-any-javascript-nkf</link>
      <guid>https://dev.to/judelawrence/turn-a-checkbox-into-a-button-without-any-javascript-nkf</guid>
      <description>&lt;p&gt;Sometimes checkboxes aren't the best UI/UX for your users, especially if you have a lot of options that you need to give to a user to choose from. &lt;/p&gt;

&lt;p&gt;This technique allows you to turn a checkbox into a button, creating a larger click area and also giving better context to what the user is selecting.&lt;/p&gt;

&lt;p&gt;NOTE: In the HTML it's important that the input comes before the label. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/Jude-Lawrence/embed/abQwNGy?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And that's it. If you have any questions or it's not clear what's going on, or even if you have a better way of achieving this, feel free to comment below.&lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>Stimulus JS - Accessing an action in another controller</title>
      <dc:creator>Jude</dc:creator>
      <pubDate>Mon, 19 Jun 2023 15:44:13 +0000</pubDate>
      <link>https://dev.to/judelawrence/stimulus-js-triggering-an-action-in-another-controller-1o5f</link>
      <guid>https://dev.to/judelawrence/stimulus-js-triggering-an-action-in-another-controller-1o5f</guid>
      <description>&lt;p&gt;If you've used Stimulus before, you should know that controllers are scoped. This can be useful, but you might come across a case where we want to trigger an action in a controller that is out of scope.&lt;/p&gt;

&lt;p&gt;We can do this with a custom event. &lt;/p&gt;

&lt;p&gt;I'll start off with a tl;dr version and will follow with an explanation &lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The javascript&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;trigger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CustomEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;event-name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dispatchEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trigger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// or js inline &lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onclick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;window.dispatchEvent(new Event('event-name'))&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- The HTML attribute --&amp;gt;&lt;/span&gt;
data-action="event-name@window-&amp;gt;controller#action"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Here's a contrived example, to give you an idea of how to get this working. Here are some circles we want to turn red when we click a button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div data-controller="controller1"&amp;gt;
  &amp;lt;button data-action="click-&amp;gt;contoller1#turnCircleRed"&amp;gt;
    Button 1
  &amp;lt;/button&amp;gt;
  &amp;lt;div 
    class="round-square" 
    data-controller1-target="circle"&amp;gt;
  &amp;lt;/div&amp;gt;    
&amp;lt;/div&amp;gt;

&amp;lt;div data-controller="controller2"&amp;gt;
  &amp;lt;button data-action="click-&amp;gt;contoller2#turnCircleRed"&amp;gt;
    Button 2
  &amp;lt;/button&amp;gt;
  &amp;lt;div 
    class="round-square" 
    data-controller2-target="circle"&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's create the controllers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/javascript/controllers/controller1&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@hotwired/stimulus&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;// Connects to data-controller="controller1"&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;targets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;circle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;

  &lt;span class="nf"&gt;turnCircleRed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;      
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;circleTarget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and here's the second one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/javascript/controllers/controller2&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@hotwired/stimulus&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;// Connects to data-controller="controller2"&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;targets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;circle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;

  &lt;span class="nf"&gt;turnCircleRed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;      
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;circleTarget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we click on either of the buttons, the background of the circle next to the button will turn red. Even though both targets are called "circle", because each stimulus controller only has access to elements inside of the div (or element) where it was declared, it cannot see or interact with the other circle.&lt;/p&gt;

&lt;p&gt;Now, what we want to happen is that when we click on the second button, we want BOTH circles to turn red.&lt;/p&gt;

&lt;p&gt;To do this, we add a custom event to controller2's turnRed action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/javascript/controllers/controller2&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@hotwired/stimulus&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;// Connects to data-controller="controller2"&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;targets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;circle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;

  &lt;span class="nf"&gt;turnRed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;        
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;circleTarget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Custom event we will use as our trigger&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;trigger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CustomEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;trigger-red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dispatchEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trigger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and to our view, we want to add an action that will listen for this event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div data-controller="controller1"&amp;gt;
  &amp;lt;button data-action="click-&amp;gt;contoller1#turnRed"&amp;gt;
    Button 1
  &amp;lt;/button&amp;gt;
  &amp;lt;div id="circle1" class="round-square" 
    data-controller1-target="circle" 
    data-action="trigger-red@window-&amp;gt;contoller1#turnRed"&amp;gt; 
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;div data-controller="controller2"&amp;gt;
  &amp;lt;button 
    id=circle2" 
    class="round-square" 
    data-action="click-&amp;gt;contoller2#turnRed"
  &amp;gt;
    Button 2
  &amp;lt;/button&amp;gt;
  &amp;lt;div class="round-square" data-controller1-target="circle"&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we click on Button 2 the following will happen&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;controller2#turnRed action will run and #circle2 will turn red.&lt;/li&gt;
&lt;li&gt;Then an event called "trigger-red" will be attached to the "window" element&lt;/li&gt;
&lt;li&gt;"trigger-red@window" will see this, and run controller1#turnRed&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  A mild word of warning
&lt;/h2&gt;

&lt;p&gt;Now we know how to do this, I think it's a good idea to use this sparingly as it does add some complexity to our code and makes us use a little more mental gymnastics to read and update our code in the future.&lt;/p&gt;

&lt;p&gt;Let me know if this was helpful, if you have any questions, or spotted some mistakes/errors or got something completely wrong.  &lt;/p&gt;

&lt;p&gt;Thanks for reading, and happy coding!&lt;/p&gt;



&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://stimulus.hotwired.dev/handbook/introduction" rel="noopener noreferrer"&gt;https://stimulus.hotwired.dev/handbook/introduction&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>stimulus</category>
    </item>
    <item>
      <title>Ruby On Rails Beginners - Demystify the magic. Part 3: The link_to method</title>
      <dc:creator>Jude</dc:creator>
      <pubDate>Wed, 26 Apr 2023 15:13:19 +0000</pubDate>
      <link>https://dev.to/judelawrence/ruby-on-rails-beginners-demystify-the-magic-part-3-the-linkto-method-9n</link>
      <guid>https://dev.to/judelawrence/ruby-on-rails-beginners-demystify-the-magic-part-3-the-linkto-method-9n</guid>
      <description>&lt;p&gt;In our two last posts, we went over how ruby methods in Rails are usually written without parentheses, and that they really are just plain old ruby methods.&lt;/p&gt;

&lt;p&gt;Now we are going to dive into the Rails documentation to get a better understanding of what the link_to helper method is used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the documentation
&lt;/h2&gt;

&lt;p&gt;Visit &lt;a href="https://edgeapi.rubyonrails.org/"&gt;https://edgeapi.rubyonrails.org/&lt;/a&gt; and bookmark this page, if you haven't already.&lt;/p&gt;

&lt;p&gt;Note: when you google Rails methods, be aware that sometimes it will take you to an older version of the API docs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gTiPAfX9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mg28nsqppqc9btq4rp9i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gTiPAfX9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mg28nsqppqc9btq4rp9i.png" alt="Image description" width="800" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the top left search field type in link_to, not only does it give us the link_to, but it also shows us there's a &lt;code&gt;link_to_if&lt;/code&gt; helper and some &lt;code&gt;link_to_unless&lt;/code&gt; helpers too! Let's stick to the link_to helper for now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to"&gt;https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Breaking it down
&lt;/h2&gt;

&lt;p&gt;To start lets look at the breakdown of an anchor tag in HTML&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0keeqClF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gp6dea08hohq4n1iuek.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0keeqClF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gp6dea08hohq4n1iuek.jpg" alt="Image description" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty straightforward.&lt;/p&gt;

&lt;p&gt;Now the Rails docs start off with this first line.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;link_to(name = nil, options = nil, html_options = nil, &amp;amp;block)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That looks OK, it's just a method that takes in 4 arguments, which are effectively 3 arguments, with the &amp;amp;block meaning when we call the method we have the option to follow it with a &lt;code&gt;do&lt;/code&gt; block (we will cover this later).&lt;/p&gt;

&lt;p&gt;Let's take a look at each of the arguments and see what they do.&lt;/p&gt;

&lt;h3&gt;
  
  
  name = nil
&lt;/h3&gt;

&lt;p&gt;In the link_to description, it says "Creates an anchor element of the given name using a URL created by the set of options."&lt;/p&gt;

&lt;p&gt;Straight off, this sounds a little bit cryptic. "name" or "options" don't really make sense in the context of an HTML anchor link.&lt;/p&gt;

&lt;p&gt;Luckily if we continue reading, we get a little bit more context.&lt;/p&gt;

&lt;p&gt;It breaks it down into the context of the actual output, so we see that &lt;br&gt;
`&lt;code&gt;link_to(name = nil, options = nil, html_options = nil, &amp;amp;block)&lt;/code&gt;&lt;br&gt;
is actually&lt;br&gt;
&lt;code&gt;link_to(body, url, html_options = {})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So the &lt;code&gt;name = nil&lt;/code&gt; is actually the body of the anchor link, ie: the text between the &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;/a&amp;gt;&lt;/code&gt; tags. &lt;/p&gt;

&lt;p&gt;Also, if it's not passed in as an argument, it defaults to nil (that's what the &lt;code&gt;= nil&lt;/code&gt; means). But when would you ever create an empty anchor element? Well, you wouldn't, but we need this for when we pass the method a block, which we will cover later.&lt;/p&gt;

&lt;h3&gt;
  
  
  options = nil
&lt;/h3&gt;

&lt;p&gt;Again, &lt;code&gt;options = nil&lt;/code&gt; doesn't really tell us much about what this is for, but looking at the example we see that the second argument is the URL &lt;code&gt;link_to(body, url, html_options = {})&lt;/code&gt;. We will usually put in a string of the URL we want or we use a path helper thats rails gives. eg: &lt;code&gt;root_path&lt;/code&gt;, which will return a string of our site's root path. Pretty easy so far.&lt;/p&gt;

&lt;h3&gt;
  
  
  html_options = nil
&lt;/h3&gt;

&lt;p&gt;Now this one is a little trickier. Even the given example doesn't really give us any more information.&lt;/p&gt;

&lt;p&gt;To figure out what this is let's actually experiment with the method in a Rails app.&lt;/p&gt;

&lt;p&gt;Let's start off with a basic link that has a body and a URL.&lt;br&gt;
&lt;code&gt;&amp;lt;%= link_to "Dev Blog", "http://dev.to" %&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will return an anchor link like this&lt;br&gt;
&lt;code&gt;&amp;lt;a href="http://dev.to"&amp;gt;Dev Blog&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;OK, so let's see what the html_options do, remembering that we need to pass in a hash, so each element has a key with a value.&lt;br&gt;
&lt;code&gt;&amp;lt;%= link_to "Dev Blog", "http://dev.to", {beep: "boop"} %&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;returns&lt;br&gt;
&lt;code&gt;&amp;lt;a beep="boop" href="http://dev.to"&amp;gt;Dev Blog&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So html_options allows us to create attributes for our anchor tag. We can add a class, an id, and any other attribute our link needs. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;%= link_to "Dev Blog", "http://dev.to", {class: "link primary", target: "_blank"} %&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;returns&lt;br&gt;
&lt;code&gt;&amp;lt;a class="link primary" target="_blank" href="http://dev.to"&amp;gt;Dev Blog&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;note: &lt;code&gt;target="_blank"&lt;/code&gt; will open the link in a new window.&lt;/p&gt;

&lt;p&gt;We can even change the request from a &lt;code&gt;GET&lt;/code&gt; to any of the other request methods. (&lt;code&gt;GET&lt;/code&gt; being the default action of a link)&lt;br&gt;
&lt;code&gt;&amp;lt;%= link_to "Delete", "listing/24",{method: :delete, class: 'btn-secondary'} %&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;returns:&lt;br&gt;
&lt;code&gt;&amp;lt;a class="btn-secondary" rel="nofollow" data-method="delete" href="/listing/24"&amp;gt;Delete&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now here you will notice that the method attribute has turned into &lt;code&gt;data-method&lt;/code&gt;, Rails sees you are changing the method attribute and automatically prefixes it with &lt;code&gt;data-&lt;/code&gt;.&lt;br&gt;
This is something that is required for Hotwire and Turbo to work. &lt;/p&gt;

&lt;p&gt;You also see that now there is a &lt;code&gt;rel="nofollow"&lt;/code&gt; attribute. Again, this is something that Rails adds for us automatically and this tells search engines to ignore the link when they are indexing your website. There's no page or content when you click on a link that sends a delete request, so there's nothing for a search engine to index.&lt;/p&gt;

&lt;p&gt;See more details here:&lt;br&gt;
&lt;a href="https://backlinko.com/nofollow-link"&gt;https://backlinko.com/nofollow-link&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &amp;amp;block
&lt;/h2&gt;

&lt;p&gt;Now this is easy to implement but a little tricky to understand how it's doing it, so today let's just stick with the implementation side of things. (Look up ruby callback methods if you want a better understanding of how this works.)&lt;/p&gt;

&lt;p&gt;Sometimes we need a link to be more than just a text link, like a button or a card component, this is where the &amp;amp;block comes in.&lt;/p&gt;

&lt;p&gt;So now when we call the link_to method, we follow it with a &lt;code&gt;do&lt;/code&gt; block, with the required HTML written inside the block.&lt;/p&gt;

&lt;p&gt;example:&lt;br&gt;
&lt;code&gt;&amp;lt;%= link_to "http://dev.to" do %&amp;gt;&lt;br&gt;
    &amp;lt;div class="card"&amp;gt;&lt;br&gt;
      This is a link block&lt;br&gt;
    &amp;lt;/div&amp;gt;&lt;br&gt;
&amp;lt;% end %&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;returns:&lt;br&gt;
&lt;code&gt;&amp;lt;a href="http://dev.to"&amp;gt;&lt;br&gt;
    &amp;lt;div class="card"&amp;gt;&lt;br&gt;
      This is a link block&lt;br&gt;
    &amp;lt;/div&amp;gt;&lt;br&gt;
&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This allows us to make card components that are links themselves. Pretty cool.&lt;/p&gt;

&lt;p&gt;It's all pretty clever stuff but can be a little confusing if you don't know what it's doing.&lt;/p&gt;

&lt;h2&gt;
  
  
  One last thing
&lt;/h2&gt;

&lt;p&gt;Just as we covered in Part 1, there's also a way of writing the link_to method arguments without wrapping the options in curly braces. So instead of writing this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;%= link_to "Dev Blog", "http://dev.to", {class: "link primary", target: "_blank"} %&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;you can write, and will quite often see this:&lt;br&gt;
&lt;code&gt;&amp;lt;%= link_to "Dev Blog", "http://dev.to", class: "link primary", target: "_blank" %&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Again, Ruby is smart enough to know that if you have a method that requires 3 arguments, and when we call it we pass in 6 arguments, all the arguments after the second argument will become a hash that gets passed in as the third argument.&lt;/p&gt;

&lt;p&gt;I prefer to use the curly braces, as it makes it more explicit when you are reading the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now we know how to use the API docs and we understand the link_to helper method a little bit better. Next, we will look at the actual source code for Rails to get an even better understanding.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Multiple Images with Rails 7: Making Images Sortable</title>
      <dc:creator>Jude</dc:creator>
      <pubDate>Thu, 26 Jan 2023 06:44:21 +0000</pubDate>
      <link>https://dev.to/judelawrence/multiple-images-with-rails-7-making-images-orderable-sortable-4cn2</link>
      <guid>https://dev.to/judelawrence/multiple-images-with-rails-7-making-images-orderable-sortable-4cn2</guid>
      <description>&lt;p&gt;Note: This post will eventually be part of a series about a complete solution for users to upload and manage multiple images that are attached to a record.&lt;/p&gt;




&lt;p&gt;At this point, you should have image hosting set up, you can upload multiple images, and they should persist when editing the record.&lt;/p&gt;

&lt;p&gt;Next, we want to add the ability to re-arrange the order of the images and for the order to persist.&lt;/p&gt;

&lt;p&gt;To do this we are going to use a few tools:&lt;br&gt;
Importmaps, stimulus, RequestJS, acts_as_list gem, and a js library called sortable_js.&lt;/p&gt;

&lt;p&gt;We will start off by setting up all of the required dependencies.&lt;/p&gt;
&lt;h2&gt;
  
  
  acts_as_list setup
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/brendon/acts_as_list" rel="noopener noreferrer"&gt;https://github.com/brendon/acts_as_list&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;gem 'acts_as_list', '~&amp;gt; 1.0.4'&lt;/code&gt; to your Gemfile, and run &lt;code&gt;bundle install&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a position column to ActiveStorageAttachments table.&lt;br&gt;
&lt;code&gt;rails g migration AddPostionColumnToActiveStorageAttachments position:integer&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is what the acts_as_list gem will use to manage the order of the images.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Crate an initializer so act_as_list can access the ActiveStorage Model
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/initializers/active_storage_acts_as_list.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;ActiveStorageAttachmentList&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Concern&lt;/span&gt;

  &lt;span class="n"&gt;included&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;acts_as_list&lt;/span&gt; &lt;span class="ss"&gt;scope: &lt;/span&gt;&lt;span class="sx"&gt;%i[record_id record_type name]&lt;/span&gt;
    &lt;span class="n"&gt;default_scope&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_prepare&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;ActiveStorage&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Attachment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt; &lt;span class="ss"&gt;:include&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ActiveStorageAttachmentList&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Don't forget to restart the server whenever you add or update your initializers.&lt;/p&gt;
&lt;h2&gt;
  
  
  sortable_js setup
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/rails/requestjs-rails" rel="noopener noreferrer"&gt;https://github.com/rails/requestjs-rails&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run &lt;code&gt;bin/importmap pin sortablejs&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create the stimulus controller. run &lt;code&gt;rails g stimulus sortable&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Setup the controller
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/assets/javascript/controllers/sortable_controller.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@hotwired/stimulus&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Sortable&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sortablejs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// Add this line, to access the sortablejs library&lt;/span&gt;
&lt;span class="c1"&gt;// Connects to data-controller="sortable"&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sortable controller connected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// I add this for testing purposes. Once everything is working you can comment this line out.&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Connect the view to the controller
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# app/views/cars/_form.html.erb
&amp;lt;div class="images-wrapper" data-controller="sortable" &amp;gt;
   &amp;lt;% @car.images.each do |image| %&amp;gt;
      &amp;lt;div class="form-image-card"&amp;gt;
         &amp;lt;%= image_tag image, class:"form-image" %&amp;gt;
      &amp;lt;/div&amp;gt;
   &amp;lt;% end %&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now when you load the page, in the browser console, you should see &lt;code&gt;Sortable controller connected&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  RequestJS Set up
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/rails/requestjs-rails" rel="noopener noreferrer"&gt;https://github.com/rails/requestjs-rails&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the requestjs-rails gem to your Gemfile: gem 'requestjs-rails'&lt;/li&gt;
&lt;li&gt;Run ./bin/bundle install.&lt;/li&gt;
&lt;li&gt;Run ./bin/rails requestjs:install&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;import { FetchRequest } from '@rails/request.js'&lt;/code&gt; to the sortable controller.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/assets/javascript/controllers/sortable_controller.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@hotwired/stimulus&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Sortable&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sortablejs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FetchRequest&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@rails/request.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Connects to data-controller="sortable"&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And that's it for setting up our dependencies. Now it's on to the code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Adding drag and drop functionality
&lt;/h2&gt;

&lt;p&gt;In our &lt;code&gt;sortable&lt;/code&gt; stimulus controller, we want to connect the parent element that contains the images we want to sort. So in our view file, we already have:&lt;br&gt;
&lt;/p&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/cars/_form.html.erb&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&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;"images-wrapper"&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"sortable"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;% @car.images.each &lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="sx"&gt;%&amp;gt;
      &amp;lt;div class="form-image-card"&amp;gt;&lt;/span&gt;
         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= image_tag image, class:"form-image" %&amp;gt;
      &amp;lt;/div&amp;gt;
   &amp;lt;% end %&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we want to create a sortable element out of the "images-wrapper" div.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/assets/javascript/controllers/sortable_controller.js&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="c1"&gt;// Connects to data-controller="sortable"&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sortable controller connected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;testing&lt;/span&gt; &lt;span class="nx"&gt;purposes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;Once&lt;/span&gt; &lt;span class="nx"&gt;everything&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;working&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;can&lt;/span&gt; &lt;span class="nx"&gt;comment&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="nx"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="c1"&gt;// 'this' is the element in the view that is connecting to this controller. &lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sortable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Sortable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, back in your view, you should now be able to drag and drop the images, to change their order. Huzzah! The only issue is that it doesn't persistent yet, so as soon as you reload the page the image order won't have saved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Persisting the order
&lt;/h2&gt;

&lt;p&gt;This is where acts_as_list gem comes into play. To start off, we will create a route that we will use to trigger the controller action that will save the updated order of the images when an image has been dragged and dropped.&lt;/p&gt;

&lt;h3&gt;
  
  
  routes
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/routes.rb&lt;/span&gt;
&lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="n"&gt;cars&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
   &lt;span class="n"&gt;member&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;patch&lt;/span&gt; &lt;span class="ss"&gt;:move_image&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give us a path of &lt;code&gt;/cars/:id/move_image&lt;/code&gt; that will run the action &lt;code&gt;move_image&lt;/code&gt; in the &lt;code&gt;car&lt;/code&gt; controller.&lt;/p&gt;

&lt;h3&gt;
  
  
  controller
&lt;/h3&gt;

&lt;p&gt;Next, we will set up the controller by adding the following action&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/controllers/cars_controller.rb&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
   &lt;span class="c1"&gt;# /cars/:id/move_image&lt;/span&gt;
   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;move_image&lt;/span&gt;
    &lt;span class="c1"&gt;#Find the car who's images we are re-arranging&lt;/span&gt;
    &lt;span class="vi"&gt;@car&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;GeneralListing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="c1"&gt;#Find the image we are moving&lt;/span&gt;
    &lt;span class="vi"&gt;@image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;images&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:old_position&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="c1"&gt;# Use the insert_at method we get from acts_as_list gem&lt;/span&gt;
    &lt;span class="vi"&gt;@image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert_at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:new_position&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="ss"&gt;:ok&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  stimulus controller
&lt;/h3&gt;

&lt;p&gt;When an image is dropped we want our stimulus controller to send the :move_image patch request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@hotwired/stimulus&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Sortable&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sortablejs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FetchRequest&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@rails/request.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Connects to data-controller="sortable"&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sortable controller connected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sortable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Sortable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;ghostClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sortable-ghost&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;chosenClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sortable-chosen&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;dragClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sortable-drag&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;onEnd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sortable onEnd triggered&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FetchRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;patch&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;urlValue&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;?old_position=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;oldIndex&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;new_position=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;newIndex&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perform&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and that should be it. The user should now be able to drag and drop images, and the order should persist.&lt;/p&gt;

&lt;p&gt;Starting out in Rails can be intimidating so if I've missed anything or if you are unsure of any of the steps, comment below and I will update the post for clarity.&lt;/p&gt;




&lt;p&gt;References&lt;br&gt;
Gorails Tutorial&lt;br&gt;
&lt;a href="https://gorails.com/episodes/sortable-drag-and-drop" rel="noopener noreferrer"&gt;https://gorails.com/episodes/sortable-drag-and-drop&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/gorails-screencasts/sortable-drag-and-drop" rel="noopener noreferrer"&gt;https://github.com/gorails-screencasts/sortable-drag-and-drop&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Gorails Forum&lt;br&gt;
&lt;a href="https://gorails.com/forum/sorting-images-using-active-storage" rel="noopener noreferrer"&gt;https://gorails.com/forum/sorting-images-using-active-storage&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Drifting Rails Tutorial - Setting up Import Maps&lt;br&gt;
&lt;a href="https://www.driftingruby.com/episodes/importmaps-in-rails-7" rel="noopener noreferrer"&gt;https://www.driftingruby.com/episodes/importmaps-in-rails-7&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code with Pete Tutorial&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=FKAMRLQpypk" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=FKAMRLQpypk&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Request JS&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=ACChhA4GdfM" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=ACChhA4GdfM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Request JS&lt;br&gt;
&lt;a href="https://github.com/rails/requestjs-rails" rel="noopener noreferrer"&gt;https://github.com/rails/requestjs-rails&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Adding acts_as_list to active storage&lt;br&gt;
&lt;a href="https://gist.github.com/kwent/050b0a580fa635e5aaa225ea3a1dd846" rel="noopener noreferrer"&gt;https://gist.github.com/kwent/050b0a580fa635e5aaa225ea3a1dd846&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sortable JS&lt;br&gt;
&lt;a href="https://sortablejs.github.io/Sortable/" rel="noopener noreferrer"&gt;https://sortablejs.github.io/Sortable/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>performance</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Ruby On Rails Beginners - Demystify the magic. Part 2: It's just a ruby method</title>
      <dc:creator>Jude</dc:creator>
      <pubDate>Thu, 12 Jan 2023 19:16:32 +0000</pubDate>
      <link>https://dev.to/judelawrence/ruby-on-rails-beginners-demystify-the-magic-part-2-its-just-a-ruby-method-1pbj</link>
      <guid>https://dev.to/judelawrence/ruby-on-rails-beginners-demystify-the-magic-part-2-its-just-a-ruby-method-1pbj</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.to/jlaw79/ruby-on-rails-beginners-demystify-the-magic-part-1-the-ruby-looks-different-4f6b"&gt;Part 1&lt;/a&gt; | &lt;a href="https://dev.to/judelawrence/ruby-on-rails-beginners-demystify-the-magic-part-3-the-linkto-method-9n"&gt;Part 3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Following on from part 1, we are going to break down the link_to method. It's something you'll use almost as soon as you start working on a rails app, and it's fairly easy to understand.&lt;/p&gt;

&lt;p&gt;The link_to method is referred to as a helper. A helper is just a ruby method that is available to use in Rails view files. Rails comes with built-in helpers, like link_to, but you can also create your own.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= link_to "Apple", "http://apple.com" %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or if you prefer to use parentheses&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= link_to("Apple", "http://apple.com") %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which will render out the following HTML code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://apple.com"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Apple&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get an idea of what's going on here, we can recreate a basic version of the method ourselves.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;link_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="s2"&gt;"&amp;lt;a href='&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;'&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/a&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;html_safe&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that it's. The built-in helper is more complex than this, but at it's most basic version this is all it does.&lt;/p&gt;

&lt;p&gt;The built in helper can also take in a third argument, which is a hash where you can pass in HTML options, which you can learn more about &lt;a href="https://api.rubyonrails.org/v5.2.3/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the next part we'll dive in to the rails api docs, and learn a little more about the link_to method.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>meteor</category>
      <category>programming</category>
    </item>
    <item>
      <title>Ruby On Rails Beginners - Demystify the magic. Part 1: The Ruby looks a little different</title>
      <dc:creator>Jude</dc:creator>
      <pubDate>Thu, 12 Jan 2023 11:23:34 +0000</pubDate>
      <link>https://dev.to/judelawrence/ruby-on-rails-beginners-demystify-the-magic-part-1-the-ruby-looks-different-4f6b</link>
      <guid>https://dev.to/judelawrence/ruby-on-rails-beginners-demystify-the-magic-part-1-the-ruby-looks-different-4f6b</guid>
      <description>&lt;p&gt;This is a series where we try and look past the Ruby 'magic' and understand how to demystify Rails, starting from 0 to reading Rails source code! Scary stuff.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/judelawrence/ruby-on-rails-beginners-demystify-the-magic-part-2-its-just-a-ruby-method-1pbj"&gt;Part 2&lt;/a&gt; | &lt;a href="https://dev.to/judelawrence/ruby-on-rails-beginners-demystify-the-magic-part-3-the-linkto-method-9n"&gt;Part 3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you first start learning Rails, it's usually after learning some basic programming in Ruby. This is still the best way to get into rails for a beginner, but making the jump can be a little bit confusing.&lt;/p&gt;

&lt;p&gt;For me, the Ruby in Ruby on Rails looked different to what I was used to when I was writing Ruby, and when I was starting out I found it hard to understand what was going on. One of the reasons is that my eyes were used to seeing method arguments wrapped in parentheses &lt;code&gt;method_name(arg1, arg2)&lt;/code&gt;. Something which you commonly don't use in rails.&lt;/p&gt;

&lt;p&gt;So to hopefully help demystify Rails for someone (anyone?) going through the same confusion, I'm going to use the link_to method, as it's one of the first things you will work within a Rails app.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is link_to
&lt;/h2&gt;

&lt;p&gt;Link_to is a method used in Ruby on Rails to create HTML links that point to a specific web address (url). It’s a powerful and flexible tool and it's an essential part of any Rails application.&lt;/p&gt;

&lt;p&gt;It takes two arguments (it can also take a third argument and a block which we will cover later), the text to be displayed, and the target URL.&lt;/p&gt;

&lt;p&gt;This is how you would usually see it, in a view file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= link_to “Home”, root_path %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what it looks like with parentheses&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;link_to("Home", root_path)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For me on my first RoR day/week, this would have made a lot more sense. I think I missed the explanation that when you call a method in ruby that requires arguments, even if you don't use parentheses, ruby is smart enough to figure out that they must be arguments, so they are treated as such.&lt;/p&gt;

&lt;p&gt;Because I missed that, I just put it down to that Rails magic that everyone keeps talking about.&lt;/p&gt;

&lt;p&gt;This may seem really silly, and maybe it is, but if this helps anyone, even just a little bit, then I'm more than happy to look a little bit silly.&lt;/p&gt;

&lt;p&gt;In part 2, I'll try and drive home that Rails is just plain old ruby, and we'll look at re-creating a basic version of the link_to method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/jlaw79/ruby-on-rails-beginners-demystify-the-magic-part-2-its-just-a-ruby-method-1pbj"&gt;part 2 &amp;gt;&amp;gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Creating an External, Bootable, MacOS Installer</title>
      <dc:creator>Jude</dc:creator>
      <pubDate>Thu, 27 Oct 2022 06:03:17 +0000</pubDate>
      <link>https://dev.to/judelawrence/creating-a-bootable-macos-installer-drive-1mb8</link>
      <guid>https://dev.to/judelawrence/creating-a-bootable-macos-installer-drive-1mb8</guid>
      <description>&lt;p&gt;For every major MacOS release, I've gotten in to the habit of formatting my computer and doing a fresh OS install. I've done this a few way over the years, but my current process starts with creating a bootable external drive I can boot from.&lt;/p&gt;

&lt;p&gt;My current process to create a bootable MacOS drive, is as follows.&lt;/p&gt;

&lt;p&gt;Step 1. Find an external USB drive with at least 16GB of free space, and plug it in. This drive will be wiped, so make sure there is no data on it that you need.&lt;/p&gt;

&lt;p&gt;Step 2. Open the terminal and enter the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;softwareupdate --list-full-installers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give you a list of available installers.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4ZKyB1XY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xv75pc31xk3anr40pxwx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4ZKyB1XY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xv75pc31xk3anr40pxwx.png" alt="Terminal" width="494" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 3. In the terminal enter the following command using the version number of the OS version you want to download.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;softwareupdate --fetch-full-installers --full-installer-version 13.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will take a while to download...&lt;/p&gt;

&lt;p&gt;Step 4. Once the download has finished, back in the terminal write the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo /Applications/Install\ macOS\ [macOS VERSION NAME].app/Contents/Resources/createinstallmedia --volume /Volumes/[USB DRIVE NAME]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace [macOS VERSION] with the version &lt;u&gt;name&lt;/u&gt; you have downloaded, and replace [USB DRIVE NAME] with the name of the  external drive you are making bootable.&lt;br&gt;
eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo /Applications/Install\ macOS\ Ventura.app/Contents/Resources/createinstallmedia --volume /Volumes/untitled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will also take a while, but once it has finished, you will now have an external drive you can boot from!&lt;/p&gt;

&lt;p&gt;To boot from an external drive, you will need to boot to the boot option screen and select the drive you just created.&lt;/p&gt;

&lt;p&gt;To get to the boot option screen:&lt;br&gt;
Intel Mac - Restart Holding down the Option key.&lt;br&gt;
Apple Silicon -Shut down the computer, then press and hold the power button.&lt;/p&gt;




&lt;p&gt;Reference&lt;br&gt;
&lt;a href="https://support.apple.com/en-us/HT201255"&gt;https://support.apple.com/en-us/HT201255&lt;/a&gt;&lt;/p&gt;

</description>
      <category>macos</category>
    </item>
  </channel>
</rss>
