<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Ruby on Tim Loughrist</title><link>https://timloughrist.com/category/ruby/</link><description>Recent content in Ruby on Tim Loughrist</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Tue, 10 Sep 2024 19:06:40 +0000</lastBuildDate><atom:link href="https://timloughrist.com/category/ruby/index.xml" rel="self" type="application/rss+xml"/><item><title>Stopping a click event without breaking your turbostream</title><link>https://timloughrist.com/2024/09/10/stopping-a-click-event-without-breaking-your-turbostream/</link><pubDate>Tue, 10 Sep 2024 19:06:40 +0000</pubDate><guid>https://timloughrist.com/2024/09/10/stopping-a-click-event-without-breaking-your-turbostream/</guid><description>&lt;p&gt;In a Rails application I have a link that has various elements within it. Some of those elements are actionable on their own while others are such that I want them to open the parent link when clicked. The elements that are actionable should not open the parent link when clicked. The link, among other things, renders a turbostream.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;%=&lt;/span&gt; link_to edit_task_path(task.id), data&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; {action&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; &lt;span style="color:#a5d6ff"&gt;&amp;#34;cli&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a5d6ff"&gt;ck-&amp;gt;drawer#showDrawer&amp;#34;&lt;/span&gt;, &lt;span style="color:#a5d6ff"&gt;&amp;#34;turbo-stream&amp;#34;&lt;/span&gt;&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; &lt;span style="color:#79c0ff"&gt;true&lt;/span&gt;}) &lt;span style="color:#ff7b72"&gt;do&lt;/span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt;div&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;%=&lt;/span&gt; render ButtonComponent.&lt;span style="color:#ff7b72"&gt;new&lt;/span&gt;(data&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; {&lt;span style="color:#a5d6ff"&gt;&amp;#34;action&amp;#34;&lt;/span&gt;&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; &lt;span style="color:#a5d6ff"&gt;&amp;#34;cli&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a5d6ff"&gt; ck-&amp;gt;selectable-table#select&amp;#34;&lt;/span&gt;}) &lt;span style="color:#ff7b72"&gt;do&lt;/span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;%&lt;/span&gt; render IconComponent.&lt;span style="color:#ff7b72"&gt;new&lt;/span&gt;(name&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; &lt;span style="color:#a5d6ff"&gt;&amp;#34;check_box&amp;#34;&lt;/span&gt;) &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;%&lt;/span&gt; end &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#f85149"&gt;/div&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt;div&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;gt;&lt;/span&gt; &#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt;span&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;gt;&amp;lt;%=&lt;/span&gt; task.title &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#f85149"&gt;/span&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#f85149"&gt;/div&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;%&lt;/span&gt; end &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As this code currently stands, clicking on the task title will open the link to the edit task path. Good. Clicking on the check box will fire the select method from the selectable-table stimulus controller. Also good.&lt;/p&gt;</description></item><item><title>Action Cable: simple implementation</title><link>https://timloughrist.com/2023/04/04/action-cable-simple-implementation/</link><pubDate>Tue, 04 Apr 2023 17:48:43 +0000</pubDate><guid>https://timloughrist.com/2023/04/04/action-cable-simple-implementation/</guid><description>&lt;p&gt;&lt;a href="https://guides.rubyonrails.org/action_cable_overview.html"&gt;Action Cable&lt;/a&gt; is a framework to allow multiple clients to receive seamless, simultaneous updates from a Rails server. If you have an application where changes submitted by one user should be seen immediately by all users without a refresh, Action Cable might be the solution you’re looking for.&lt;/p&gt;&#10;&lt;p&gt;In my case, I was developing a &lt;a href="https://github.com/tloughrist/capstone-frontend"&gt;two to four player variant of Chess&lt;/a&gt; where all players needed to be able to see every move made by the other players. Since all players are being served by the same server, they could see the new state of the board by refreshing their page, but that is clearly not an ideal way to handle the issue.&lt;/p&gt;</description></item><item><title>Friendship associations with rails</title><link>https://timloughrist.com/2023/01/26/friendship-associations-with-rails/</link><pubDate>Thu, 26 Jan 2023 16:13:44 +0000</pubDate><guid>https://timloughrist.com/2023/01/26/friendship-associations-with-rails/</guid><description>&lt;p&gt;Let’s say we’re building an event scheduling application. We want our users to be able to form friendships with one another, create events, and invite friends to those events. Implementing these functions poses some interesting challenges when using Rails and Active Record. In this post I’ll work through those challenges.&lt;/p&gt;&#10;&lt;p&gt;It is worth noting up front that we don’t need to rebuild the wheel here. There are plenty of gems that will allow you to easily implement friendship relationships, including:&lt;/p&gt;</description></item><item><title>Going beyond table data with Sinatra</title><link>https://timloughrist.com/2022/11/10/going-beyond-table-data-with-sinatra/</link><pubDate>Thu, 10 Nov 2022 11:32:56 +0000</pubDate><guid>https://timloughrist.com/2022/11/10/going-beyond-table-data-with-sinatra/</guid><description>&lt;p&gt;&lt;a href="https://github.com/sinatra/sinatra#serving-a-modular-application"&gt;Sinatra&lt;/a&gt; is a domain specific programming language designed to allow the HTTP request handling capacity of a web application to be built quickly and easily. Sinatra’s routes elegantly and eloquently combine an HTTP method, e.g., ‘get’, ‘post’, ‘patch’, and ‘delete’, with a URL matching pattern so that when the relevant HTTP request is made to the relevant URL, a block of code is executed.&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://guides.rubyonrails.org/active_record_basics.html"&gt;Active Record&lt;/a&gt; is an &lt;a href="http://en.wikipedia.org/wiki/Object-relational_mapping"&gt;object-relational mapper&lt;/a&gt;, linking the objects of a web-application to the data contained in the related database’s tables.&lt;/p&gt;</description></item></channel></rss>