<?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: not-a-patch</title>
    <description>The latest articles on DEV Community by not-a-patch (@notapatch).</description>
    <link>https://dev.to/notapatch</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%2F1521%2F1710795.png</url>
      <title>DEV Community: not-a-patch</title>
      <link>https://dev.to/notapatch</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/notapatch"/>
    <language>en</language>
    <item>
      <title>Rubymine running Rails with Foreman</title>
      <dc:creator>not-a-patch</dc:creator>
      <pubDate>Fri, 25 Mar 2022 18:55:36 +0000</pubDate>
      <link>https://dev.to/notapatch/rubymine-running-rails-with-foreman-363a</link>
      <guid>https://dev.to/notapatch/rubymine-running-rails-with-foreman-363a</guid>
      <description>&lt;p&gt;Rails 7 has made &lt;code&gt;foreman&lt;/code&gt; the standard way of running Rails. Rubymine's default project configuration will only start the Rails server and not the additional applications in the &lt;code&gt;Procfile.dev&lt;/code&gt;. The Rubymine documentation solves the problem with RVM and gemsets but what if you don't use RVM? How can you get Rubymine to start via Foreman?&lt;/p&gt;

&lt;p&gt;At the command line make sure you have foreman. &lt;a href="https://github.com/ddollar/foreman/wiki/Don%27t-Bundle-Foreman" rel="noopener noreferrer"&gt;You should not add foreman to the Gemfile!&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gem install foreman&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You then create a new configuration for running or debugging the application. RubyMine doesn't have a configuration for Foreman so we will have to make it ourselves.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In the main menu, go to Run | Edit Configurations, click the Add icon, and select Gem Command from the list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the Run/Debug Configurations dialog, specify the following settings:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&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%2F3q4udsc1su3v8p9gqq3h.png" 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%2F3q4udsc1su3v8p9gqq3h.png" alt="Run/Debug Configurations dialog configured for foreman"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Name: Enter the run/debug configuration name, for example, foreman start.&lt;/li&gt;
&lt;li&gt;Gem name: Specify the foreman gem here.&lt;/li&gt;
&lt;li&gt;Executable name: Select the foreman executable.&lt;/li&gt;
&lt;li&gt;Arguments: Specify an argument for running the application using Foreman, for example, start.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Click on Bundler&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%2Ftquaqsjl22mopy4uow8b.png" 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%2Ftquaqsjl22mopy4uow8b.png" alt="Run/Debug Configurations dialog Bundler tab"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Disable bundle exec

&lt;ul&gt;
&lt;li&gt;bundle exec stops you finding Gems outside of the Gemfile bundle. If you've followed the instructions faithfully then foreman won't be in the bundle.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Click OK.&lt;/p&gt;

&lt;p&gt;You should now be able to run the &lt;code&gt;foreman start&lt;/code&gt; configuration from within Rubymine.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="http://ddollar.github.io/foreman/" rel="noopener noreferrer"&gt;Foreman manual&lt;/a&gt;&lt;br&gt;
&lt;a href="https://bundler.io/man/bundle-exec.1.html" rel="noopener noreferrer"&gt;Bundler - bundle exec&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Thanks
&lt;/h3&gt;

&lt;p&gt;Thanks to the Reddit Rails community and jakefillsbass for the tip!&lt;/p&gt;

</description>
      <category>rubymine</category>
      <category>foreman</category>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Switching off RSpec generators </title>
      <dc:creator>not-a-patch</dc:creator>
      <pubDate>Wed, 02 Feb 2022 16:56:43 +0000</pubDate>
      <link>https://dev.to/notapatch/switching-off-rspec-generators-59p1</link>
      <guid>https://dev.to/notapatch/switching-off-rspec-generators-59p1</guid>
      <description>&lt;p&gt;Finding the correct switch to turn off RSpec generators can be hard as it is sparsely documented. &lt;a href="https://github.com/rspec/rspec-rails/blob/5-1-maintenance/lib/generators/rspec/scaffold/scaffold_generator.rb"&gt;However, RSpec has a number of generators that are listed in this file.&lt;/a&gt;&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;# rspec-rails: lib/generators/rspec/scaffold/scafold_generator.rb&lt;/span&gt;
&lt;span class="n"&gt;class_option&lt;/span&gt; &lt;span class="ss"&gt;:controller_specs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;type: :boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;default: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;desc: &lt;/span&gt;&lt;span class="s2"&gt;"Generate controller specs"&lt;/span&gt;
&lt;span class="n"&gt;class_option&lt;/span&gt; &lt;span class="ss"&gt;:request_specs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="ss"&gt;type: :boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;default: &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;desc: &lt;/span&gt;&lt;span class="s2"&gt;"Generate request specs"&lt;/span&gt;
&lt;span class="n"&gt;class_option&lt;/span&gt; &lt;span class="ss"&gt;:view_specs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="ss"&gt;type: :boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;default: &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;desc: &lt;/span&gt;&lt;span class="s2"&gt;"Generate view specs"&lt;/span&gt;
&lt;span class="n"&gt;class_option&lt;/span&gt; &lt;span class="ss"&gt;:helper_specs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="ss"&gt;type: :boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;default: &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;desc: &lt;/span&gt;&lt;span class="s2"&gt;"Generate helper specs"&lt;/span&gt;
&lt;span class="n"&gt;class_option&lt;/span&gt; &lt;span class="ss"&gt;:routing_specs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="ss"&gt;type: :boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;default: &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;desc: &lt;/span&gt;&lt;span class="s2"&gt;"Generate routing specs"&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then use the list to switch off generators in &lt;code&gt;application.rb&lt;/code&gt;.&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="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generators&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;g&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test_framework&lt;/span&gt; &lt;span class="ss"&gt;:rspec&lt;/span&gt;
  &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;controller_specs&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;
  &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;request_specs&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;
  &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;view_specs&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;
  &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;helper_specs&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;
  &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routing_specs&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>`form_with` option change from `local: false` to `local: true`</title>
      <dc:creator>not-a-patch</dc:creator>
      <pubDate>Sat, 29 Jan 2022 15:26:56 +0000</pubDate>
      <link>https://dev.to/notapatch/formwith-option-change-from-local-false-to-local-true-1pmb</link>
      <guid>https://dev.to/notapatch/formwith-option-change-from-local-false-to-local-true-1pmb</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;form_with&lt;/code&gt; was &lt;a href="https://guides.rubyonrails.org/5_1_release_notes.html#unification-of-form-for-and-form-tag-into-form-with"&gt;introduced in Rails 5.1&lt;/a&gt; to avoid the confusion of having &lt;code&gt;form_for&lt;/code&gt; and &lt;code&gt;form_tag&lt;/code&gt; having similar roles.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;form_with&lt;/code&gt;, was to have by default forms submitted with AJAX, xhr, rather than the standard HTTP request. The difference is that with a HTTP request, the current window is cleared, and the server response is loaded into the window. While an AJAX request, the current window is unaffected and the page can be updated depending on the results of the request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rails changes to &lt;code&gt;form_with&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Rails 5.1 documentation of &lt;code&gt;form_with&lt;/code&gt; options &lt;a href="https://apidock.com/rails/v5.1.7/ActionView/Helpers/FormHelper/form_with"&gt;described the situation as&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;:local - By default form submits are remote and unobstrusive XHRs. Disable remote submits with local: true.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, by Rails 6.1 the situation has reversed as &lt;code&gt;form_with&lt;/code&gt; was now local by default. &lt;a href="https://apidock.com/rails/v6.1.3.1/ActionView/Helpers/FormHelper/form_with"&gt;This is described in the documentation as:&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;:local - By default form submits via typical HTTP requests. Enable remote and unobtrusive XHRs submits with local: false. Remote forms may be enabled by default by setting &lt;code&gt;config.action_view.form_with_generates_remote_forms = true&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Verify
&lt;/h3&gt;

&lt;p&gt;With all this confusion, it's good to verify that what you think you're doing is actually happening. In the code we add &lt;code&gt;local: false&lt;/code&gt; meaning this is a remote form to be submitted by AJAX.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example Code (&lt;a href="https://guides.rubyonrails.org/working_with_javascript_in_rails.html#form-with"&gt;from 5.1 release notes&lt;/a&gt;)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;form_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;model: &lt;/span&gt;&lt;span class="vi"&gt;@article&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;local: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&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;form&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
  ...
&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Produced code
&lt;/h4&gt;

&lt;p&gt;If we view the HTML of the page the produced code includes &lt;code&gt;data-remote="true"&lt;/code&gt;. We have an AJAX form.&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;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/articles"&lt;/span&gt; &lt;span class="err"&gt;...&lt;/span&gt; &lt;span class="na"&gt;data-remote=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  ...
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Motivation for writing
&lt;/h3&gt;

&lt;p&gt;I keep getting confused about what happened when, and I'm writing this as reference to me. I do like the &lt;a href="https://apidock.com/rails/v5.1.7/ActionView/Helpers/FormHelper/form_with"&gt;api dock&lt;/a&gt; which makes it easier to see what happened. Obviously, if I've misunderstood something, please feel to correct me. I wouldn't want to make the situation any worse!!&lt;/p&gt;

&lt;h3&gt;
  
  
  Reading
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://guides.rubyonrails.org/5_1_release_notes.html#unification-of-form-for-and-form-tag-into-form-with"&gt;2.8 Unification of form_for and form_tag into form_with&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Rubymine: Enable the JavaScript debugger</title>
      <dc:creator>not-a-patch</dc:creator>
      <pubDate>Thu, 09 Dec 2021 17:31:20 +0000</pubDate>
      <link>https://dev.to/notapatch/rubymine-enable-the-javascript-debugger-170b</link>
      <guid>https://dev.to/notapatch/rubymine-enable-the-javascript-debugger-170b</guid>
      <description>&lt;h3&gt;
  
  
  Enable the JavaScript debugger
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Open the Run/Debug Configuration by navigating from the main menu: 

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Run =&amp;gt; Edit Configurations&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Enable &lt;code&gt;run browser&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Enter the start URL&lt;/li&gt;
&lt;li&gt;Enable Start JavaScript debugger automatically when debugging.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B0Y2w9Nt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfrf26g4zjn50z5ncs2g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B0Y2w9Nt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfrf26g4zjn50z5ncs2g.png" alt="Run/Debug Configuration" width="791" height="680"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why did I write this?
&lt;/h3&gt;

&lt;p&gt;This is taken from RubyMine documentation, so why write this?&lt;/p&gt;

&lt;p&gt;I wanted to be able to enable the JavaScript debugger within RubyMine, RubyMine 2021.3, and I had no luck until I found this &lt;a href="https://www.jetbrains.com/help/ruby/debug-javascript-in-a-rails-application.html"&gt;RubyMine tutorial&lt;/a&gt;. It took me ages to Google and still can't find it straight away, so I'm leaving this note to myself.&lt;/p&gt;

</description>
      <category>rubymine</category>
      <category>javascript</category>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Rails going backwards</title>
      <dc:creator>not-a-patch</dc:creator>
      <pubDate>Mon, 20 Sep 2021 11:22:26 +0000</pubDate>
      <link>https://dev.to/notapatch/rails-going-backwards-56h5</link>
      <guid>https://dev.to/notapatch/rails-going-backwards-56h5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is a Rails review on how to link back to the page you've just visited from. I had struggled to get a solution, and reading through Stackoverflow I don't think I'm alone in this. This is my take and I hope it is useful or can become useful with the help of others.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Solution 1: Back with hardcoding&lt;/li&gt;
&lt;li&gt;Solution 2: Back with referer&lt;/li&gt;
&lt;li&gt;Solution 3: Back with JavaScript&lt;/li&gt;
&lt;li&gt;Solution 4: Back with session&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Solution 1: Back with hardcoding
&lt;/h2&gt;

&lt;p&gt;If there's only one route in and out of the page. Hardcoding the path out is a clean option. From here, it gets more interesting...&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 2: Back with referer
&lt;/h2&gt;

&lt;p&gt;A popular back solution is to use Rails &lt;a href="https://api.rubyonrails.org/v6.1.4/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to" rel="noopener noreferrer"&gt;&lt;code&gt;:back&lt;/code&gt; symbol to be passed to link_to&lt;/a&gt;. An example is in listing 1.&lt;/p&gt;

&lt;h4&gt;
  
  
  Listing 1
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;

&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;link_to&lt;/span&gt; &lt;span class="s2"&gt;"my link', :back &lt;/span&gt;&lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://github.com/rails/rails/blob/main/actionview/lib/action_view/helpers/url_helper.rb#L39" rel="noopener noreferrer"&gt;link_to :back rails code&lt;/a&gt; It is described in the docs as:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Using a :back Symbol instead of an options hash will generate a link to the referer (a JavaScript back link will be used in place of a referer if none exists).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What's a referer? MDN give a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer" rel="noopener noreferrer"&gt;definition for referer&lt;/a&gt; they say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Referer HTTP request header contains an absolute or partial address of the page making the request. When following a link, this would be the address of the page containing the link.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, requests often have a URL of the page you were last on. This is the referer, &lt;a href="https://en.wikipedia.org/wiki/HTTP_referer#Etymology" rel="noopener noreferrer"&gt;spelt referer in official documents&lt;/a&gt;, and Rails uses it to get back again. However, the referer can be missing if the user typed the URL into the search bar directly, &lt;a href="https://stackoverflow.com/questions/6880659/in-what-cases-will-http-referer-be-empty" rel="noopener noreferrer"&gt;as well as other exceptions&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Checking for the referer
&lt;/h3&gt;

&lt;p&gt;I wanted to see how it works. First, I found a way to get the referer in Firefox. You can check the referer by going to the Firefox inspector and then the network tab and searching for referer. In this example, we have navigated to edit page and checking for referer, which is the show page (FYI: the resource was feedbacks).&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%2Fisitut94oliucwplifyl.png" 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%2Fisitut94oliucwplifyl.png" alt="What is the referer?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are two issues with using the referer:&lt;br&gt;
a. Back twice - if you go back more than once, the referer will point to where you were last. Not where you were when you were last on that page!&lt;br&gt;
b. POST - if you perform a &lt;code&gt;POST&lt;/code&gt; on the page, the referer is also reset.&lt;/p&gt;

&lt;h4&gt;
  
  
  a. Back Twice
&lt;/h4&gt;

&lt;p&gt;We can see, in table 1, that back is fine for step 1 but on step 2 going back ends up in a loop between &lt;code&gt;Show&lt;/code&gt; to &lt;code&gt;Edit&lt;/code&gt;. In other words, the Referer only remembers one hop and can quickly form a loop because it is always pointing to where you were on the last hop. &lt;/p&gt;

&lt;h5&gt;
  
  
  Table 1: Stepping through an application, and what happens if you go back?
&lt;/h5&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;Page&lt;/th&gt;
&lt;th&gt;Referer&lt;/th&gt;
&lt;th&gt;Back&lt;/th&gt;
&lt;th&gt;Back again&lt;/th&gt;
&lt;th&gt;And again&lt;/th&gt;
&lt;th&gt;And again&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;Home&lt;/td&gt;
&lt;td&gt;Empty&lt;/td&gt;
&lt;td&gt;→ Empty&lt;/td&gt;
&lt;td&gt;→ Empty&lt;/td&gt;
&lt;td&gt;→ Empty&lt;/td&gt;
&lt;td&gt;→ Empty&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Show&lt;/td&gt;
&lt;td&gt;Home&lt;/td&gt;
&lt;td&gt;→ Home&lt;/td&gt;
&lt;td&gt;→ Empty&lt;/td&gt;
&lt;td&gt;→ Empty&lt;/td&gt;
&lt;td&gt;→ Empty&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Edit&lt;/td&gt;
&lt;td&gt;Show&lt;/td&gt;
&lt;td&gt;→ Show&lt;/td&gt;
&lt;td&gt;→ Edit&lt;/td&gt;
&lt;td&gt;⇾ Show&lt;/td&gt;
&lt;td&gt;→ Edit*&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;It will keep looping between show and edit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another way to express this information is in a diagram where we can see the black lines as user clicks and blue lines when the user back clicks.&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%2F6gn9gw7pw79iag67cozg.png" 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%2F6gn9gw7pw79iag67cozg.png" alt="Back using referer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  b. POST
&lt;/h4&gt;

&lt;p&gt;It did not work when you are &lt;code&gt;POST&lt;/code&gt; a form on the page and then want to go back. If I had to, &lt;code&gt;POST&lt;/code&gt; on the show page, say I added a comment and stayed on the &lt;code&gt;Show&lt;/code&gt; page. After you &lt;code&gt;POST&lt;/code&gt; the referer gets set to the &lt;code&gt;show&lt;/code&gt; page, the current page, and back will take you to the &lt;code&gt;show&lt;/code&gt; page. In other words, nowhere. &lt;/p&gt;

&lt;h4&gt;
  
  
  Back with referer summary
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;link_to :back&lt;/code&gt; is only useful when you want to go back one page. In our case, once it had reached edit, it won't remember the original route it got to show. Further, when you &lt;code&gt;POST&lt;/code&gt; on the page, the referer can get reset to the current page and losing the route back.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 3: Back with JavaScript
&lt;/h2&gt;

&lt;p&gt;Another popular class of answers are &lt;a href="https://stackoverflow.com/a/46609048/1511504" rel="noopener noreferrer"&gt;JavaScript&lt;/a&gt;. JavaScript is also used in &lt;a href="https://github.com/rails/rails/blob/main/actionview/lib/action_view/helpers/url_helper.rb#L48" rel="noopener noreferrer"&gt;Rails &lt;code&gt;:back&lt;/code&gt; if the referer can't be found.&lt;/a&gt; Example of Back with Javascript is in listing 2.&lt;/p&gt;

&lt;h4&gt;
  
  
  Listing 2
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;

 &lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;link_to&lt;/span&gt; &lt;span class="s1"&gt;'Back'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'javascript:history.back()'&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;JavaScript back uses the browser's page stack. Each time it visits a page, it is added to a stack of URLs. Going back meant popping each page reference off the stack. &lt;a href="https://stackoverflow.com/a/1313941/1511504" rel="noopener noreferrer"&gt;Here's a Stackoverflow on how a Browser back button works&lt;/a&gt;. The downside for this, if the user had gone to the edit and then saved and returned to the show page and &lt;em&gt;then&lt;/em&gt; started to use back you would have to go "back" via the edit which might not be what the user expected.&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%2F4y98ycfui169tzdcm82q.png" 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%2F4y98ycfui169tzdcm82q.png" alt="Back using JavaScript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 4: Back with session
&lt;/h2&gt;

&lt;p&gt;The final class of solutions were &lt;a href="https://stackoverflow.com/a/16213741/1511504" rel="noopener noreferrer"&gt;saving the routes in the session&lt;/a&gt;. To get this to work, I added methods to the &lt;code&gt;ApplicationController&lt;/code&gt; to set and retrieve the values, as well as code to update the session in the controller and use the session variable in the view's link_to - Listing 3 contains the code.&lt;/p&gt;

&lt;p&gt;This is not nearly as nice as the other two solutions, much less cohesion, as there's already 3 files required compared to 1 liners for the other solutions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I'm not saying this is good code, I'm just saying this is what I had to do to get it to work!&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Listing 3 Example of using session to go backwards
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="c1"&gt;# app/controllers/application_controller.rb&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationController&lt;/span&gt;&lt;br&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;&lt;br&gt;
  &lt;span class="n"&gt;helper_method&lt;/span&gt; &lt;span class="ss"&gt;:retrieve_last_index_page_or_default&lt;/span&gt;&lt;br&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;&lt;br&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;store_last_index_page&lt;/span&gt;&lt;br&gt;
    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:last_index_page&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fullpath&lt;/span&gt;&lt;br&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;retrieve_last_index_page_or_default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;default_path: &lt;/span&gt;&lt;span class="n"&gt;root_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;br&gt;
    &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:last_index_page&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;default_path&lt;/span&gt;&lt;br&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;&lt;br&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;&lt;br&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Updating session in controller code&lt;br&gt;
&lt;/h4&gt;
&lt;br&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="c1"&gt;# app/controllers/my_controller.rb&lt;/span&gt;&lt;br&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;&lt;br&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;&lt;br&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;br&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;&lt;br&gt;
    &lt;span class="c1"&gt;# this could have been in a before_action&lt;/span&gt;&lt;br&gt;
    &lt;span class="n"&gt;store_last_index_page&lt;/span&gt;&lt;br&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;&lt;br&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;&lt;br&gt;
&lt;span class="k"&gt;end&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  View code&lt;br&gt;
&lt;/h4&gt;
&lt;br&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;
&lt;h1&gt;
  
  
  app/views/my/index.html.erb
&lt;/h1&gt;
&lt;h1&gt;
  
  
  this could have been a variable set in controller
&lt;/h1&gt;

&lt;p&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;link_to&lt;/span&gt; &lt;span class="n"&gt;retrieve_last_index_page_or_default&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;br&gt;
  ...&lt;br&gt;
&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Summary&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;There are four ways to go backwards:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;link_to&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;link_to my_path&lt;/td&gt;
&lt;td&gt;Hardcoded&lt;/td&gt;
&lt;td&gt;Clean, limited to one route&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;link_to :back&lt;/td&gt;
&lt;td&gt;Referer&lt;/td&gt;
&lt;td&gt;Clean, limited to one hop&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;link_to 'javascript:history.back()'&lt;/td&gt;
&lt;td&gt;JavaScript&lt;/td&gt;
&lt;td&gt;Access to Browser back&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;link_to get_session_variable()&lt;/td&gt;
&lt;td&gt;Session&lt;/td&gt;
&lt;td&gt;Powerful but complicated&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;p&gt;&lt;a href="https://api.rubyonrails.org/v6.1.4/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to" rel="noopener noreferrer"&gt;link_to Rails docs (6.1.4)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/rails/rails/blob/main/actionview/lib/action_view/helpers/url_helper.rb#L39" rel="noopener noreferrer"&gt;url_helper Rails code (6.1.4)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer" rel="noopener noreferrer"&gt;MDN - Referer&lt;/a&gt;&lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/HTTP_referer#Etymology" rel="noopener noreferrer"&gt;HTTP_referer#Etymology&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/6880659/in-what-cases-will-http-referer-be-empty" rel="noopener noreferrer"&gt;In what cases will the Referer be empty?&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/a/1313941/1511504" rel="noopener noreferrer"&gt;How does the Back button in a web browser work?&lt;/a&gt;&lt;/p&gt;

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