<?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: Akshay Khot</title>
    <description>The latest articles on DEV Community by Akshay Khot (@software_writer).</description>
    <link>https://dev.to/software_writer</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%2F32006%2F2778fb6f-e615-4bc4-a362-b2c0ec5e2132.jpeg</url>
      <title>DEV Community: Akshay Khot</title>
      <link>https://dev.to/software_writer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/software_writer"/>
    <language>en</language>
    <item>
      <title>Understanding the Rails Router: Why, What, and How</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Fri, 17 Mar 2023 06:37:37 +0000</pubDate>
      <link>https://dev.to/software_writer/understanding-the-rails-router-why-what-and-how-21lc</link>
      <guid>https://dev.to/software_writer/understanding-the-rails-router-why-what-and-how-21lc</guid>
      <description>&lt;p&gt;Ever since the day I started learning Ruby on Rails, routing remained kind of a mystery. The basic 20% stuff is very simple and intuitive, but the remaining 80% techniques used by advanced Rails programmers seemed outright magical, and hence, very daunting.&lt;/p&gt;

&lt;p&gt;Over the weekend, I did a deep-dive into the Rails router to understand it better, and summarized my notes in a post that I just published on my blog, which you can find below. I hope you’ll find it helpful and that you’ll learn something new.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.akshaykhot.com/understanding-rails-router-why-what-how/"&gt;Understanding the Rails Router: Why, What, and How&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It turned out to be way, wayy, wayyy longer than I anticipated (&amp;gt; 5000 words), so don’t expect to read it in one sitting. However, if you can stick through it and read it till the end, I can pretty much guarantee that you'll have a much better understanding of the Rails router.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
    </item>
    <item>
      <title>How Cross-Site Request Forgery Works</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Wed, 14 Sep 2022 03:43:44 +0000</pubDate>
      <link>https://dev.to/software_writer/how-cross-site-request-forgery-works-51jg</link>
      <guid>https://dev.to/software_writer/how-cross-site-request-forgery-works-51jg</guid>
      <description>&lt;p&gt;A Cross-Site Request Forgery (CSRF) attack tricks authenticated users into performing a dangerous activity on the application, such as transferring funds, changing the credentials, granting access to some protected resource, etc. Since the users are authenticated on the application, it doesn't prevent them from performing this activity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q5aVspBc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/it48k2fadwhce3uf4sr2.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q5aVspBc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/it48k2fadwhce3uf4sr2.jpeg" alt="CSRF Attack" width="880" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Open Web Application Security Project, or OWASP, describes this vulnerability as follows. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cross-Site Request Forgery (CSRF) is an attack that forces an end user to execute unwanted actions on a web application in which they’re currently authenticated. With a little help of social engineering (such as sending a link via email or chat), an attacker may trick the users of a web application into executing actions of the attacker’s choosing. &lt;/p&gt;

&lt;p&gt;If the victim is a normal user, a successful CSRF attack can force the user to perform state-changing requests like transferring funds, changing their email address, etc. CSRF can compromise the entire web application if the victim is an administrative account.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Here's a scenario that demonstrates how a CSRF attack works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step One:&lt;/strong&gt; An attacker creates a fraud request that performs some dangerous activity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step Two:&lt;/strong&gt; The attacker embeds this request into a hyperlink or a form and posts these links in many places on the internet, where unsuspecting users are likely to click them, such as forum comments, images, email links, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step Three:&lt;/strong&gt; A victim is logged onto the application (e.g. banking website) in one tab and opens the email in the other. Then they click the fraud link in the email. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step Four:&lt;/strong&gt; Most web applications implement authentication using browser cookies. Cookies have nothing to do with tabs or windows -- they have to do with requests to a domain. Whenever the browser requests the web server for a domain, any cookies that the browser has for that domain will be sent in the request header. Since the user was logged into the application in the first tab, the browser sends the cookies from the request made from the second tab. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step Five:&lt;/strong&gt; Upon receiving the request, the web application checks the cookies and authenticates the user. Now it's thinking you're performing this request. However, this is the forged request created by the attacker. This request can now do anything that your application allows to logged-in users, such as transferring money from the bank account.  &lt;/p&gt;

&lt;p&gt;Now, you might think that clicking a link or opening an image will make a GET request, which is harmless. Well, the attacker can easily write JavaScript code that creates and submits a form on the fly upon clicking a link or visiting a web page.&lt;/p&gt;

&lt;p&gt;Here's an example that creates a form making a POST request to update your bank email and submits the form whenever the page loads.&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;"https://your-bank.com/user/email"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"malicious-email@example.com"&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;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forms&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;submit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, all the hacker needs to do is trick you into visiting the page while you are logged into your bank website. Once the page loads, it will submit the form to the bank's website, passing the cookies. The bank website will think you are a genuine user and updates the email address. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to Prevent CSRF?
&lt;/h2&gt;

&lt;p&gt;So the main problem behind the CSRF attack is that the server application is not differentiating between a genuine request (from the actual application) vs. the forged request that came from the attacker's code. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Only if there was a way for the server to identify the requests that came from the application, then it could reject all other requests that came from anywhere else. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can solve this problem by instructing the server application to insert a unique, random token inside each form. Now, when the form is submitted from the application, this token is sent along with it. Upon receiving the request, the server checks if this token is present and matches the token it has. If it's present and matches, it knows that the request came from a genuine user and is valid. It rejects the request if the token is missing or doesn't match. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fwry5Uxx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5z9ca2tbo6p13bipc5t.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fwry5Uxx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5z9ca2tbo6p13bipc5t.jpeg" alt="Preventing CSRF using Auth Tokens" width="880" height="589"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the token is random, the attacker can't guess and insert it into their forged requests. Hence the server rejects all forged requests, keeping the users safe. &lt;/p&gt;

&lt;p&gt;This is how most web application frameworks prevent CSRF attacks, by using a token that verifies the authenticity. If you'd like to read more in detail about how Ruby on Rails does this, you can read this detailed article on my blog: &lt;a href="https://www.akshaykhot.com/understanding-authenticity-tokens-in-rails/"&gt;Preventing CSRF Attacks using Authenticity Tokens in Rails&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope this helps. Please let me know if you have any questions or comments. I look forward to your feedback.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>rails</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Check if a Variable is Defined in Ruby</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Wed, 10 Aug 2022 03:20:00 +0000</pubDate>
      <link>https://dev.to/software_writer/how-to-check-if-a-variable-is-defined-in-ruby-35df</link>
      <guid>https://dev.to/software_writer/how-to-check-if-a-variable-is-defined-in-ruby-35df</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published on my blog: &lt;a href="https://www.akshaykhot.com/how-to-check-if-a-variable-is-defined-in-ruby/"&gt;How to Check if a Variable is Defined in Ruby&lt;br&gt;
&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;Ruby provides a handy &lt;code&gt;defined?(expression)&lt;/code&gt; keyword that tests if the &lt;code&gt;expression&lt;/code&gt; refers to anything recognizable. The &lt;code&gt;expression&lt;/code&gt; can be an object, a variable that's initialized, method name, etc. If Ruby can't resolve the expression, it returns &lt;code&gt;nil&lt;/code&gt;. Otherwise, it returns a string describing the expression. &lt;/p&gt;

&lt;p&gt;Here are some examples of using &lt;code&gt;defined?&lt;/code&gt; with different types of expressions. Note that a variable set to &lt;code&gt;nil&lt;/code&gt; is still initialized and recognized by ruby.&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="no"&gt;RSpec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;describe&lt;/span&gt; &lt;span class="s1"&gt;'Defined'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s1"&gt;'tests if the local variable is defined'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Akshay'&lt;/span&gt;

    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'local-variable'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'nil'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'constant'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'expression'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s1"&gt;'ensures that a variable set to nil is still recognized'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'local-variable'&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using with conditional assignment
&lt;/h3&gt;

&lt;p&gt;Sometimes, you want to lazily evaluate some code, only once. That is, do nothing if a variable exists but initialize it if it doesn't. The idiomatic ruby approach is to use the &lt;code&gt;||=&lt;/code&gt; operator.&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;result&lt;/span&gt;
  &lt;span class="vi"&gt;@result&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="n"&gt;calculate_result&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;calculate_result&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s1"&gt;'&amp;gt;&amp;gt;&amp;gt; heavy calculation here.. should happen only once'&lt;/span&gt;
  &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s1"&gt;'lazy-evaluates the calculate_result operation once'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# Output&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;heavy&lt;/span&gt; &lt;span class="n"&gt;calculation&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;happen&lt;/span&gt; &lt;span class="n"&gt;only&lt;/span&gt; &lt;span class="n"&gt;once&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just make sure you don't use it with an operation that can return &lt;code&gt;nil&lt;/code&gt; or boolean value &lt;code&gt;false&lt;/code&gt; as the result. Otherwise, it will invoke the &lt;code&gt;calculate_result&lt;/code&gt; every time, eliminating the benefit of &lt;code&gt;||=&lt;/code&gt; operator. &lt;/p&gt;

&lt;p&gt;For example, if you change the &lt;code&gt;calculate_result&lt;/code&gt; method above to return &lt;code&gt;false&lt;/code&gt; (or &lt;code&gt;nil&lt;/code&gt;) instead of 100, ruby will call &lt;code&gt;calculate_result&lt;/code&gt; each time &lt;code&gt;result&lt;/code&gt; is called.&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;calculate_result&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s1"&gt;'&amp;gt;&amp;gt;&amp;gt; heavy calculation here.. should happen only once'&lt;/span&gt;
  &lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="c1"&gt;# or nil&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# Output&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;heavy&lt;/span&gt; &lt;span class="n"&gt;calculation&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;happen&lt;/span&gt; &lt;span class="n"&gt;only&lt;/span&gt; &lt;span class="n"&gt;once&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;heavy&lt;/span&gt; &lt;span class="n"&gt;calculation&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;happen&lt;/span&gt; &lt;span class="n"&gt;only&lt;/span&gt; &lt;span class="n"&gt;once&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;defined?&lt;/code&gt; method comes in handy in such cases. Change the &lt;code&gt;result&lt;/code&gt; method so it first checks if the &lt;code&gt;@result&lt;/code&gt; variable is defined.&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;result&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="vi"&gt;@result&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="vi"&gt;@result&lt;/span&gt;
  &lt;span class="vi"&gt;@result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calculate_result&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Don't use &lt;code&gt;defined?&lt;/code&gt; to check hash keys
&lt;/h3&gt;

&lt;p&gt;A common mistake is to use &lt;code&gt;defined?&lt;/code&gt; to verify if a hash key is defined. For example,&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;check_hash_key&lt;/span&gt;
  &lt;span class="nb"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="k"&gt;defined?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'key'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'unexpected!'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'not defined'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s1"&gt;'does not return false for non-existing hash key'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;check_hash_key&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'unexpected!'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because it returns the string &lt;code&gt;method&lt;/code&gt;, which ruby evaluates to &lt;code&gt;true&lt;/code&gt; in a boolean expression.&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;it&lt;/span&gt; &lt;span class="s1"&gt;'returns method for non-existing hash key'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'key'&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'method'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The idiomatic ruby solution to check if a key exists in a hash is to use any of the following methods: &lt;code&gt;has_key?&lt;/code&gt;, &lt;code&gt;key?&lt;/code&gt;, &lt;code&gt;include?&lt;/code&gt;, or &lt;code&gt;member?&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;key?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'key'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use parenthesis when using &lt;code&gt;defined?&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;You don't always need to use them, but it's highly recommended due to the low precedence of &lt;code&gt;defined?&lt;/code&gt; keyword. For example, if you want to check if a variable exists and it's greater than zero, you might write something like this.&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;it&lt;/span&gt; &lt;span class="s1"&gt;'has low precedence'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# Fails!&lt;/span&gt;
&lt;span class="c1"&gt;# expected: true&lt;/span&gt;
&lt;span class="c1"&gt;# got: "expression"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding parentheses results in a better check, and it's also very clear. The following test passes with flying colors.&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;it&lt;/span&gt; &lt;span class="s1"&gt;'has low precedence'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'expression'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defined?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ruby</category>
    </item>
    <item>
      <title>Reverse Method Refactoring</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Tue, 12 Jul 2022 16:39:30 +0000</pubDate>
      <link>https://dev.to/software_writer/reverse-method-refactoring-2kb</link>
      <guid>https://dev.to/software_writer/reverse-method-refactoring-2kb</guid>
      <description>&lt;p&gt;Here's a handy refactoring technique I learned today while reading Kent Beck's &lt;a href="https://learning.oreilly.com/library/view/smalltalk-best-practice/9780132852098/"&gt;Smalltalk Best Practice Patterns&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But first, what's the problem?&lt;/strong&gt; Sometimes, you have an unreadable method because you are calling different methods on different objects. You cannot extract a method because it's already small (but unreadable) and does one thing. &lt;/p&gt;

&lt;p&gt;Here's a simplified example.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;', '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Though the example is not complicated, it takes a while to understand what's going on and who's calling whom. &lt;/p&gt;

&lt;p&gt;We can solve the problem by making sure that all messages go through a single object. Kent provides the following approach for this refactoring: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Code a method on the parameter. &lt;/li&gt;
&lt;li&gt;Derive its name from the original message. &lt;/li&gt;
&lt;li&gt;Take the original receiver as a parameter to the new method. &lt;/li&gt;
&lt;li&gt;Implement the method by sending the original message to the original receiver.
&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Stream&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;', '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though we introduced an additional object, the &lt;code&gt;print&lt;/code&gt; method is now easy to read and understand. &lt;/p&gt;

&lt;p&gt;What do you think?&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>Tailwind + Browsersync Sandbox</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Sat, 09 Jul 2022 00:29:33 +0000</pubDate>
      <link>https://dev.to/software_writer/tailwind-browsersync-sandbox-4po9</link>
      <guid>https://dev.to/software_writer/tailwind-browsersync-sandbox-4po9</guid>
      <description>&lt;p&gt;If you are tired of changing a Tailwind class, doing &lt;code&gt;alt + tab&lt;/code&gt;, and reloading the whole app to see the change your made, check out my &lt;a href="https://github.com/akshayKhot/design" rel="noopener noreferrer"&gt;Tailwind + Browsersync&lt;/a&gt; project template. &lt;/p&gt;

&lt;p&gt;Treat this as your local sandbox to quickly build React or Turbo Frame (if using Hotwire) component UI with Tailwind. &lt;strong&gt;The browser reloads automatically after you make a change and save.&lt;/strong&gt; Just like Tailwind playground. &lt;/p&gt;

&lt;p&gt;Once you're satisfied with a design, copy + paste it in your main project. I found this workflow very productive, compared to making a small change, and reloading my big rails app. Hope you do, too.&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%2F1i83xgq9hr9lpqv0hm23.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%2F1i83xgq9hr9lpqv0hm23.png" alt="Tailwind + Browsersync"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about &lt;a href="https://play.tailwindcss.com/" rel="noopener noreferrer"&gt;Tailwind playground&lt;/a&gt;?&lt;/strong&gt; it is great, but I do like to work in my favorite IDE with my favorite fonts and themes.&lt;/p&gt;




&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/akshayKhot/design.git your_project_name
&lt;span class="nb"&gt;cd &lt;/span&gt;your_project_name
yarn &lt;span class="c"&gt;# or npm install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;Run these commands in separate terminal tabs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn tailwind
yarn server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're using npm, run these commands instead. However, I prefer and recommend using yarn, it's very fast compared to npm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run tailwind
npm run server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a quick demo using the above set up, where I build Github's navbar. &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/zvUg7KpqJ0Y"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on my blog at &lt;a href="https://akshaykhot.com/tailwind-with-browsersync/" rel="noopener noreferrer"&gt;https://akshaykhot.com/tailwind-with-browsersync/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>tailwindcss</category>
      <category>design</category>
    </item>
    <item>
      <title>The Rails Configuration File (~/.railsrc)</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Thu, 23 Jun 2022 04:05:11 +0000</pubDate>
      <link>https://dev.to/software_writer/the-rails-configuration-file-railsrc-4f4c</link>
      <guid>https://dev.to/software_writer/the-rails-configuration-file-railsrc-4f4c</guid>
      <description>&lt;p&gt;&lt;strong&gt;Did you know that Rails has a &lt;code&gt;~/.railsrc&lt;/code&gt; file?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Similar to your &lt;code&gt;~/.bashrc&lt;/code&gt; or &lt;code&gt;~/.zshrc&lt;/code&gt; file, you can use this file to configure your Rails applications. &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%2Fn34lvzurl128rhd6z4eh.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%2Fn34lvzurl128rhd6z4eh.png" alt="The Rails Configuration File"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is especially useful if you find yourself repeatedly typing &lt;code&gt;--skip&lt;/code&gt; or &lt;code&gt;--no-skip&lt;/code&gt; commands and installing specific gems every time you create a new Rails application.&lt;/p&gt;

&lt;p&gt;Since I made a switch to Rails from .NET last year, I must have created at least 20-30 projects for either learning, experiments, or client work. I have a text file that documents all the gems I want to install for a fresh Rails app. Every time I create a new project, I go through the file to install all the gems I want for my project. &lt;/p&gt;

&lt;p&gt;With the &lt;code&gt;~/.railsrc&lt;/code&gt; file, I don't need to do that. Rails will do it for me. Here's how you do this. &lt;/p&gt;

&lt;p&gt;First, create the &lt;code&gt;.railsrc&lt;/code&gt; file in your home directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch&lt;/span&gt; ~/.railsrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add whatever options you want in this file. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;--database&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;mysql
&lt;span class="nt"&gt;--skip-active-job&lt;/span&gt;
&lt;span class="nt"&gt;--skip-spring&lt;/span&gt;
&lt;span class="nt"&gt;--skip-javascript&lt;/span&gt;

&lt;span class="nt"&gt;--template&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;~/dotfiles/rails_template.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To see all the available options, type &lt;code&gt;rails&lt;/code&gt; in a non-rails directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;➜  rails rails
Usage:
  rails new APP_PATH &lt;span class="o"&gt;[&lt;/span&gt;options]

Options:
      &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;--skip-namespace&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;, &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;--no-skip-namespace&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;              &lt;span class="c"&gt;# Skip namespace (affects only isolated engines)&lt;/span&gt;
      &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;--skip-collision-check&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;, &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;--no-skip-collision-check&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;  &lt;span class="c"&gt;# Skip collision check&lt;/span&gt;
  &lt;span class="nt"&gt;-r&lt;/span&gt;, &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;--ruby&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;PATH]                                          &lt;span class="c"&gt;# Path to the Ruby binary of your choice&lt;/span&gt;
                                                             &lt;span class="c"&gt;# Default: /Users/akshay/.rbenv/versions/3.1.0/bin/ruby&lt;/span&gt;
  &lt;span class="nt"&gt;-m&lt;/span&gt;, &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;--template&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;TEMPLATE]                                  &lt;span class="c"&gt;# Path to some application template (can be a filesystem path or URL)&lt;/span&gt;
  &lt;span class="nt"&gt;-d&lt;/span&gt;, &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;--database&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;DATABASE]                                  &lt;span class="c"&gt;# Preconfigure for selected database &lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;To pre-configure the gems you'd like to install, create a &lt;code&gt;template.rb&lt;/code&gt; file. Here's mine:&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;gem_group&lt;/span&gt; &lt;span class="ss"&gt;:development&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:test&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'dotenv-rails'&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'factory_bot_rails'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;gem_group&lt;/span&gt; &lt;span class="ss"&gt;:development&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'better_errors'&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'binding_of_caller'&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'annotate'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add the path to the template at the end of your &lt;code&gt;~/.railsrc&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;--template&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;~/software/rails/template.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. The next time you run &lt;code&gt;rails new app&lt;/code&gt;, Rails will use the configuration file along with the &lt;code&gt;Gemfile&lt;/code&gt; template to create your application just like you want. &lt;/p&gt;

&lt;p&gt;Pretty cool, right?&lt;/p&gt;




&lt;p&gt;This post was originally published on my blog at &lt;a href="https://akshaykhot.com/railsrc-rails-configuration-file/" rel="noopener noreferrer"&gt;https://akshaykhot.com/railsrc-rails-configuration-file/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Ruby Shortcut to Call a Method with Each Array Item as Parameter</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Sat, 11 Jun 2022 00:26:30 +0000</pubDate>
      <link>https://dev.to/software_writer/ruby-shortcut-to-call-a-method-with-each-array-item-as-parameter-29fp</link>
      <guid>https://dev.to/software_writer/ruby-shortcut-to-call-a-method-with-each-array-item-as-parameter-29fp</guid>
      <description>&lt;p&gt;As a Ruby programmer, you probably know the &lt;code&gt;&amp;amp;:&lt;/code&gt; shortcut to call a method on each item in an array. For example, this snippet will call the &lt;code&gt;to_s&lt;/code&gt; method on &lt;code&gt;:foo&lt;/code&gt; and &lt;code&gt;:bar&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="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:bar&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:to_s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code is similar to:&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="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:bar&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;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;item&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_s&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, instead of calling a method on the array item, you want to &lt;strong&gt;call a method and pass the array item to that method.&lt;/strong&gt; Is there a one-liner for that?&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="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:bar&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;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;item&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It turns out, Ruby has a corresponding &lt;code&gt;&amp;amp;method&lt;/code&gt; shortcut to accomplish precisely this.&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="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:bar&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:puts&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things are going on here.&lt;/p&gt;

&lt;p&gt;First, the &lt;code&gt;method(:puts)&lt;/code&gt; finds the method named &lt;code&gt;puts&lt;/code&gt;. As per the documentation, you can access the instance variables and &lt;code&gt;self&lt;/code&gt; method on whichever object you called &lt;code&gt;method&lt;/code&gt; on. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://ruby-doc.org/core-3.1.1/Object.html#method-i-method"&gt;method(sym) -&amp;gt; method&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looks up the named method as a receiver in &lt;em&gt;obj&lt;/em&gt;, returning a &lt;a href="https://ruby-doc.org/core-3.1.1/Method.html"&gt;&lt;code&gt;Method&lt;/code&gt;&lt;/a&gt; object (or raising &lt;a href="https://ruby-doc.org/core-3.1.1/NameError.html"&gt;&lt;code&gt;NameError&lt;/code&gt;&lt;/a&gt;). The &lt;a href="https://ruby-doc.org/core-3.1.1/Method.html"&gt;&lt;code&gt;Method&lt;/code&gt;&lt;/a&gt; object acts as a closure in &lt;em&gt;obj&lt;/em&gt;'s object instance, so instance variables and the value of &lt;code&gt;self&lt;/code&gt; remain available.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Second, the &lt;code&gt;Method&lt;/code&gt; object returned by the &lt;code&gt;method&lt;/code&gt; method (&lt;em&gt;wow, that's one too many methods&lt;/em&gt;) implements the &lt;code&gt;to_proc&lt;/code&gt; method, so you can use it after &lt;code&gt;&amp;amp;&lt;/code&gt;, which is to say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If &lt;code&gt;foo&lt;/code&gt; is an object with a &lt;code&gt;to_proc&lt;/code&gt; method, then you can pass it to a method as &lt;code&gt;&amp;amp;foo&lt;/code&gt;, which will call &lt;code&gt;foo.to_proc&lt;/code&gt; and use that as the method's block.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:puts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# is equivalent to&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="c1"&gt;# is equivalent to&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you say &lt;code&gt;&amp;amp;method(:puts)&lt;/code&gt;, it finds the &lt;code&gt;Method&lt;/code&gt; object and calls &lt;code&gt;.to_proc&lt;/code&gt; on it.&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;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is equivalent to,&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;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&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;h2&gt;
  
  
  Is It Worth It?
&lt;/h2&gt;

&lt;p&gt;Now, does that mean you go ahead and replace the explicit method calls in &lt;code&gt;each&lt;/code&gt; loops with this one-liner? &lt;strong&gt;Probably not.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Sometimes, that explicit method call is just what you need to keep the code readable and easier to understand. Not just for your colleagues, but for the future you. Using this one-liner adds an extra mental overhead for your brain to translate and understand what it's doing, which is not worth in many cases. &lt;/p&gt;

&lt;p&gt;However, sometimes this shortcut is precisely what you need to express what you meant. That's why I love Ruby. The freedom to choose, do, and say whatever you want, however you want, to &lt;strong&gt;write software, well.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Anyway, hope that helps. &lt;/p&gt;

</description>
      <category>ruby</category>
    </item>
    <item>
      <title>How to Easily Navigate Directories in the Terminal</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Tue, 07 Jun 2022 00:16:56 +0000</pubDate>
      <link>https://dev.to/software_writer/how-to-make-your-cds-less-painful-3od0</link>
      <guid>https://dev.to/software_writer/how-to-make-your-cds-less-painful-3od0</guid>
      <description>&lt;p&gt;You know about the &lt;code&gt;PATH&lt;/code&gt; environment variable. Did you know about &lt;code&gt;CDPATH&lt;/code&gt;? If you've been typing long &lt;code&gt;cd&lt;/code&gt; commands like me, this post is for you. &lt;/p&gt;

&lt;p&gt;I came across this gem this morning, while reading Daniel Barrett's new book &lt;a href="https://learning.oreilly.com/library/view/efficient-linux-at/9781098113391/"&gt;Efficient Linux at the Command Line&lt;/a&gt;. It instructs the &lt;code&gt;cd&lt;/code&gt; command to search for the directory you specify in locations other than your current directory. &lt;/p&gt;

&lt;p&gt;A &lt;code&gt;cd&lt;/code&gt; search path works like your command search path &lt;code&gt;$PATH&lt;/code&gt;. However, instead of finding commands, it finds the subdirectories. You can configure it with the shell variable &lt;code&gt;CDPATH&lt;/code&gt;, and it has the same format as the &lt;code&gt;PATH&lt;/code&gt; variable: a list of directories separated by colons.&lt;/p&gt;

&lt;p&gt;Set this variable in your &lt;code&gt;~/.zshconfig&lt;/code&gt; or &lt;code&gt;~/.bashrc&lt;/code&gt; file to include the directories you visit most frequently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;CDPATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;:&lt;span class="nv"&gt;$HOME&lt;/span&gt;/software:&lt;span class="nv"&gt;$HOME&lt;/span&gt;/software/ruby:&lt;span class="nv"&gt;$HOME&lt;/span&gt;/software/rails:&lt;span class="nv"&gt;$HOME&lt;/span&gt;/software/youtube
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, whenever you want to &lt;code&gt;cd&lt;/code&gt; into a directory, the shell will look through all the above places in addition to the current directory. &lt;/p&gt;

&lt;p&gt;What's more, the search is lightning fast. It looks only in parent directories that you specify and nothing else. &lt;/p&gt;

&lt;p&gt;For example, let's say you have a &lt;code&gt;$HOME/software/blog&lt;/code&gt; directory and you've configured the &lt;code&gt;CDPATH&lt;/code&gt; to include the &lt;code&gt;$HOME/software&lt;/code&gt; directory. &lt;/p&gt;

&lt;p&gt;Now, if you type &lt;code&gt;cd blog&lt;/code&gt; from anywhere in the filesystem, the &lt;code&gt;cd&lt;/code&gt; command will take you to the &lt;code&gt;$HOME/software/blog&lt;/code&gt; directory, unless it finds another &lt;code&gt;blog&lt;/code&gt; directory in another pre-configured path. So the order of &lt;code&gt;CDPATH&lt;/code&gt; matters. If two directories in &lt;code&gt;$CDPATH&lt;/code&gt; have a subdirectory named &lt;code&gt;blog&lt;/code&gt;, the earlier parent wins.&lt;/p&gt;

&lt;p&gt;In the above example, &lt;code&gt;cd&lt;/code&gt; will check the existence of the following directories in order, until it finds one or it fails.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; in the current directory&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$HOME/software/blog&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$HOME/software/ruby/blog&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$HOME/software/rails/blog&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$HOME/software/youtube/blog&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To summarize,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Set &lt;code&gt;CDPATH&lt;/code&gt; with your most important or frequently used parent directories, and you can directly &lt;code&gt;cd&lt;/code&gt; into them or their subdirectories no matter where you are in the file system. You don't have to type the full path anymore. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Trust me, this is pretty freaking cool. It has changed the way I use the shell. &lt;/p&gt;

</description>
      <category>linux</category>
    </item>
    <item>
      <title>The Definitive Guide to Rack for Ruby and Rails Developers</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Wed, 25 May 2022 00:40:58 +0000</pubDate>
      <link>https://dev.to/software_writer/the-definitive-guide-to-rack-for-ruby-and-rails-developers-41n3</link>
      <guid>https://dev.to/software_writer/the-definitive-guide-to-rack-for-ruby-and-rails-developers-41n3</guid>
      <description>&lt;p&gt;You've been around in the Rails world for a while. You know your way around rails. But you keep hearing this word 'Rack' and don't really understand what it is or what it does for you. You try to read the documentation on the Rack Github &lt;a href="https://github.com/rack/rack" rel="noopener noreferrer"&gt;repository&lt;/a&gt; or the &lt;a href="https://guides.rubyonrails.org/rails_on_rack.html" rel="noopener noreferrer"&gt;Rails on Rack&lt;/a&gt; guides, but the only thing it does is add to the confusion. &lt;/p&gt;

&lt;p&gt;Everyone keeps saying that it provides a &lt;strong&gt;minimal&lt;/strong&gt;, &lt;strong&gt;modular&lt;/strong&gt;, and &lt;strong&gt;adaptable&lt;/strong&gt; interface for building web applications. &lt;strong&gt;But what the heck does that really mean?&lt;/strong&gt; &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%2Fw0xf6jrl4k1dl1us84w6.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%2Fw0xf6jrl4k1dl1us84w6.png" alt="Rack logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If there's a list of topics that confuses most new Rails developers, Rack is definitely up there at the top. When I started learning Ruby and Rails last year, Rack took a really long time to wrap my head around. If you are in the same boat, fear not. &lt;/p&gt;

&lt;p&gt;In this article, I will try to explain pretty much everything that you need to know about Rack as a Ruby and Rails developer. It covers everything from the basics to more advanced stuff such as middleware and writing your custom middleware. It's quite a long article, but if you stick till the end, you will know more about Rack than pretty much everyone else.&lt;/p&gt;

&lt;p&gt;But first, before we try to understand the theory behind Rack, let's try to use it in a real application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building a Web Application without Rails
&lt;/h2&gt;

&lt;p&gt;Create a new directory and add a file named &lt;code&gt;config.ru&lt;/code&gt;. Don't worry, it's just a regular Ruby file with a different extension (it's short for &lt;code&gt;rackup&lt;/code&gt;, but let's ignore that for now).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;web
&lt;span class="nb"&gt;cd &lt;/span&gt;web 

&lt;span class="nb"&gt;touch &lt;/span&gt;config.ru
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add the following code in this file, which creates the most basic web application you have seen. Don't worry about the &lt;code&gt;run&lt;/code&gt; method at the bottom for now. We will return to it later.&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;# config.ru&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;
   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="s1"&gt;'Content-Type'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'text/html'&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

     &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'&amp;lt;h1&amp;gt;Greetings from Rack!!&amp;lt;/h1&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

     &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&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="n"&gt;run&lt;/span&gt; &lt;span class="no"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is our very own web application that returns a simple response. Now let's try to run it in the browser. For that, we will need a web server.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A web server is the software that accepts HTTP requests and returns HTTP response. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I will use &lt;a href="https://github.com/puma/puma" rel="noopener noreferrer"&gt;Puma&lt;/a&gt;, which is the web server that Ruby on Rails ships with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Puma&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install and launch Puma using the following commands from the &lt;code&gt;web&lt;/code&gt; directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;➜ gem &lt;span class="nb"&gt;install &lt;/span&gt;puma 

➜ puma
Puma starting &lt;span class="k"&gt;in &lt;/span&gt;single mode...
&lt;span class="k"&gt;*&lt;/span&gt; Puma version: 5.6.4 &lt;span class="o"&gt;(&lt;/span&gt;ruby 3.1.0-p0&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Birdie's Version"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;*&lt;/span&gt;  Min threads: 0
&lt;span class="k"&gt;*&lt;/span&gt;  Max threads: 5
&lt;span class="k"&gt;*&lt;/span&gt;  Environment: development
&lt;span class="k"&gt;*&lt;/span&gt;          PID: 32026
&lt;span class="k"&gt;*&lt;/span&gt; Listening on http://0.0.0.0:9292
Use Ctrl-C to stop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, Puma looks for a &lt;code&gt;config.ru&lt;/code&gt; file in the same directory, and uses that to launch our web application. &lt;/p&gt;

&lt;p&gt;Now our server is up and running. Point your browser to &lt;a href="http://localhost:9292/" rel="noopener noreferrer"&gt;http://localhost:9292&lt;/a&gt; and you should see something like this:&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%2Flppw5q7zt4azwg3cwazt.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%2Flppw5q7zt4azwg3cwazt.png" alt="Running Application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stop the server using &lt;code&gt;Ctrl + c&lt;/code&gt; command. &lt;/p&gt;

&lt;p&gt;Now let's run our application using a different server. We'll use &lt;a href="https://github.com/macournoyer/thin" rel="noopener noreferrer"&gt;Thin&lt;/a&gt;, a small, simple, and fast web server. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install and launch the Thin web server using the following commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gem &lt;span class="nb"&gt;install &lt;/span&gt;thin
thin start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Thin server is up and running and serving our application. Point your browser to &lt;a href="http://localhost:3000/" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; and you should see the same web page that we saw earlier.&lt;/p&gt;

&lt;p&gt;Let's try one last time to use a different web server. This time we'll use &lt;a href="https://yhbt.net/unicorn/" rel="noopener noreferrer"&gt;Unicorn&lt;/a&gt;, an old web server that used to be popular in the Rails community. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unicorn&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install and launch the Unicorn web server using the following commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gem &lt;span class="nb"&gt;install &lt;/span&gt;unicorn
unicorn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Point your browser to &lt;a href="http://localhost:8080/" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt;. Again, you should see the same web page. &lt;/p&gt;

&lt;p&gt;A few questions you might have at this point: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;How did all of these web server know how to run our application?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why didn't we have to change even a single line in our application to make it work with different servers?&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The answer is that they all are &lt;strong&gt;rack-compliant&lt;/strong&gt; web servers. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Okay, but what does that mean?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It means, when started, they all looked for an application (Ruby class or object) which satisfied the following three conditions: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It has a &lt;code&gt;call&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;It accepts the &lt;code&gt;env&lt;/code&gt; object representing the HTTP request.&lt;/li&gt;
&lt;li&gt;It returns an array containing three values: the status code, the headers, and the response. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is what everyone means when they say &lt;strong&gt;&lt;em&gt;"Rack provides a minimal, modular, and adaptable interface."&lt;/em&gt;&lt;/strong&gt; You can use any class or object that satisfies the above three conditions with any web server. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every rack compliant webserver will always invoke a &lt;code&gt;call&lt;/code&gt; method on an object (the Rack application) and serve the result of that method. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The other way works, too. You can replace our simple application with another script, a Rails application, or even a Sinatra application, and any Rack-compliant web server can run it without a problem. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rack allows application frameworks &amp;amp; web servers to communicate with each other, and replace each without changing the other.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is Rack's main benefit. It provides a common protocol (or interface, or specification) that different web servers can use to talk to different web applications, without worrying about the internals of each.&lt;/p&gt;

&lt;p&gt;Before Rack came along, each framework had to understand each other web server's API to communicate with it. With Rack, the creators of web servers and web frameworks all agreed to talk to each other using a standardized way to reduce the efforts involved with communicating with different standards. &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%2Fk80aj9h2fwp40ywaejud.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%2Fk80aj9h2fwp40ywaejud.png" alt="Benefits of Rack"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are building the next high-performant web server or application framework, you can talk with any other rack-compliant web application or web server as long as you follow the Rack specification. &lt;/p&gt;

&lt;p&gt;This is pretty useful. Now let's learn some theory.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Rack Specification
&lt;/h2&gt;

&lt;p&gt;Here's the standard definition of Rack that you have heard.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rack provides a minimal, modular, and adaptable interface for developing web applications in Ruby. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At the very basic level, Rack is a protocol, just like HTTP. Rack provides a layer between the framework (Rails) &amp;amp; the web server (Puma), allowing them to communicate with each other. &lt;/p&gt;

&lt;p&gt;The Rack protocol allows you to wrap HTTP requests and responses in simple interfaces. This simplifies the API for web servers and frameworks into a single method call. &lt;/p&gt;

&lt;p&gt;This is exactly similar to how your browser talks to the web server using the HTTP protocol. That allows any client (Chrome, Firefox, or even terminal commands) to talk to any backend server written in any language (PHP, .NET, Ruby, etc.) so you can access the website.&lt;/p&gt;

&lt;p&gt;When we say an application is Rack-compliant, it means three things: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It has a &lt;code&gt;call&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;call&lt;/code&gt; method accepts a single argument &lt;code&gt;env&lt;/code&gt;, containing all the data about the request.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;call&lt;/code&gt; method returns an array containing the following:

&lt;ul&gt;
&lt;li&gt;The status code, e.g. 200 for success&lt;/li&gt;
&lt;li&gt;A hash containing the headers, and&lt;/li&gt;
&lt;li&gt;An array containing a single string, which is the response body.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The Rack Gem
&lt;/h2&gt;

&lt;p&gt;So far, we've only talked about the Rack specification. But there's also a gem called &lt;a href="https://rubygems.org/gems/rack" rel="noopener noreferrer"&gt;Rack&lt;/a&gt;. You can find the source code for it on &lt;a href="https://github.com/rack/rack" rel="noopener noreferrer"&gt;Github&lt;/a&gt;, read the documentation on &lt;a href="https://rubydoc.info/github/rack/rack" rel="noopener noreferrer"&gt;Rubydoc&lt;/a&gt;, and install it like any other Ruby gem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gem &lt;span class="nb"&gt;install &lt;/span&gt;rack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why do we need the Rack gem if the rack-compliant web servers and frameworks can communicate without it?&lt;/strong&gt; Here are a few big reasons: &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Middleware Toolbox
&lt;/h3&gt;

&lt;p&gt;Because the Rack interface is so simple, you can use any code that implements this interface in a Rack application. This allows you to build small, focused, and reusable applications which work together to provide different functionalities. These mini-components are known as &lt;strong&gt;Middleware&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Middleware sits between the user and the application code. When an HTTP request comes in, the middleware can intercept, examine, and modify it. Similarly, it can examine and modify the HTTP response before forwarding it to the user. &lt;/p&gt;

&lt;p&gt;Middleware is very useful for writing logic that is not specific to your web application, such as authenticating the request, logging, or exception handling. It focuses on doing one thing and doing it well. Using middleware also simplifies your application code, and it can only focus on the logic related to the application. &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%2F3vnrispw0yko9b1oiseh.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%2F3vnrispw0yko9b1oiseh.png" alt="Middleware"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example, here's a middleware that logs to the console&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;before it passes the request to the next application in the pipeline and &lt;/li&gt;
&lt;li&gt;after it receives the response on its way out.
&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Received the incoming request"&lt;/span&gt;

    &lt;span class="c1"&gt;# forward the request to the next middleware or app &lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Received the outgoing response"&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;This middleware accepts the &lt;code&gt;app&lt;/code&gt; in the constructor. The &lt;code&gt;app&lt;/code&gt; can be another middleware in the pipeline or the actual application. &lt;/li&gt;
&lt;li&gt;When &lt;strong&gt;call&lt;/strong&gt;ed, it first deals with the incoming request represented by the &lt;code&gt;env&lt;/code&gt; object. Here, we print "Received the incoming request". &lt;/li&gt;
&lt;li&gt;After handling the request, it passes the request to the next middleware (or app) in the pipeline,&lt;/li&gt;
&lt;li&gt;Upon receiving the response (&lt;code&gt;status, headers, body&lt;/code&gt;) from the next middleware (&lt;code&gt;@app&lt;/code&gt;), it logs to the console.&lt;/li&gt;
&lt;li&gt;Finally, it passes the response to the next middleware in the pipeline. This can be the middleware that called our logging middleware, or the web server. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's a diagram that will help you visualize the middleware pipeline. &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%2Ff86wlai2uxhlpvhasskg.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%2Ff86wlai2uxhlpvhasskg.png" alt="Middleware Pipeline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Rack gem provides many such components that you can use. All these components use the same Rack interface.&lt;/p&gt;

&lt;p&gt;Here are some middleware components included in the Rack gem. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Rack::Files&lt;/code&gt;, for serving static files.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::Config&lt;/code&gt;, for modifying the environment before processing the request.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::ContentLength&lt;/code&gt;, for setting content-length header based on body size.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::ContentType&lt;/code&gt;, for setting default content-type header for responses.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::Deflater&lt;/code&gt;, for compressing responses with gzip.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::Head&lt;/code&gt;, for returning an empty body for HEAD requests.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::Logger&lt;/code&gt;, for setting a logger to handle logging errors.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::Reloader&lt;/code&gt;, for reloading files if they have been modified.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::ShowException&lt;/code&gt;, for catching unhandled exceptions and presenting them in a nice and helpful way with clickable 'backtrace.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::ShowStatus&lt;/code&gt;, for using nice error pages for empty client error responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find a complete list &lt;a href="https://github.com/rack/rack/tree/main/lib/rack" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Pick and choose whatever you like, in any way you wish.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Tools to Build Rack Applications and Middleware
&lt;/h3&gt;

&lt;p&gt;The Rack gem provides the infrastructure code that you can use to build your own web application, web framework, or middleware. It allows to quickly prototype or build stuff without doing the same repetitive tasks again and again. &lt;/p&gt;

&lt;p&gt;Here are some helpers you can use with Rack:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Rack::Request&lt;/code&gt;, which also provides query string parsing and multipart handling.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::Response&lt;/code&gt;, for convenient generation of HTTP replies and cookie handling.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::MockRequest&lt;/code&gt; and &lt;code&gt;Rack::MockResponse&lt;/code&gt; for efficient and quick testing of Rack application without real HTTP round-trips.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::Cascade&lt;/code&gt;, for trying additional Rack applications if an application returns a &lt;code&gt;not found (404)&lt;/code&gt; or &lt;code&gt;method not supported (405)&lt;/code&gt; response.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::Directory&lt;/code&gt;, for serving files under a given directory, with directory indexes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::MediaType&lt;/code&gt;, for parsing content-type headers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Rack::Mime&lt;/code&gt;, for determining content-type based on file extension.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since the Rack interface is so simple, anyone can build and publish useful middleware, and the community did build many such components. The &lt;a href="https://github.com/rack/rack-contrib" rel="noopener noreferrer"&gt;&lt;code&gt;rack-contrib&lt;/code&gt;&lt;/a&gt; project on Github contains a library of these middleware components. Take a look; you will definitely find something useful. &lt;/p&gt;

&lt;h3&gt;
  
  
  3. The &lt;code&gt;rackup&lt;/code&gt; command
&lt;/h3&gt;

&lt;p&gt;The Rack gem ships with the &lt;code&gt;rackup&lt;/code&gt; command. It is a useful tool for running Rack applications with a web server. It uses the &lt;code&gt;Rack::Builder&lt;/code&gt; DSL (domain-specific language) to configure middleware and easily compose applications.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rackup&lt;/code&gt; automatically figures out the environment it is run in, and runs your application as WEBrick, Puma, or any web server—all from the same configuration. Let's try that now. &lt;/p&gt;

&lt;p&gt;So far, we've been launching our web application using a web server-specific command, i.e. &lt;code&gt;puma&lt;/code&gt;, &lt;code&gt;thin start&lt;/code&gt;, or &lt;code&gt;unicorn&lt;/code&gt;. Using &lt;code&gt;rackup&lt;/code&gt; lets us be web server agnostic. In the same directory containing your &lt;code&gt;config.ru&lt;/code&gt; file, run &lt;code&gt;rackup&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;➜ rackup

Puma starting &lt;span class="k"&gt;in &lt;/span&gt;single mode...
&lt;span class="k"&gt;*&lt;/span&gt; Puma version: 5.6.4 &lt;span class="o"&gt;(&lt;/span&gt;ruby 2.7.1-p83&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Birdie's Version"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;*&lt;/span&gt;  Min threads: 0
&lt;span class="k"&gt;*&lt;/span&gt;  Max threads: 5
&lt;span class="k"&gt;*&lt;/span&gt;  Environment: development
&lt;span class="k"&gt;*&lt;/span&gt;          PID: 28318
&lt;span class="k"&gt;*&lt;/span&gt; Listening on http://127.0.0.1:9292
&lt;span class="k"&gt;*&lt;/span&gt; Listening on http://[::1]:9292
Use Ctrl-C to stop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since I have Puma installed, it launched the application in Puma. If you have some other web server, &lt;code&gt;rackup&lt;/code&gt; will use that. &lt;/p&gt;

&lt;h3&gt;
  
  
  4. Convenient DSL
&lt;/h3&gt;

&lt;p&gt;If you aren't familiar with a DSL, it stands for &lt;strong&gt;Domain Specific Language&lt;/strong&gt;. It is a programming language with a higher level of abstraction optimized for a specific class of problems. A DSL uses the concepts and rules from the field or domain. For example, Ruby on Rails provides a DSL for building web applications.&lt;/p&gt;

&lt;p&gt;Rack provides such a DSL consisting of the following methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;run&lt;/code&gt;: It takes a Ruby object responding to method &lt;strong&gt;call&lt;/strong&gt; as an argument and invokes the &lt;strong&gt;call&lt;/strong&gt; method on it. We used this method in our example at the beginning of this post. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;map&lt;/code&gt;: It takes a string and a block as parameters and maps incoming requests against the string. If it matches, Rack will run the block to handle that request. This is similar to how routing works in Rails. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;use&lt;/code&gt;: It includes the middleware to the rack application.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additionally, the &lt;code&gt;Rack::Builder&lt;/code&gt; class helps you with iteratively composing Rack applications. &lt;/p&gt;

&lt;p&gt;Let's use all these tools to build another web application. First, replace the code in the &lt;code&gt;config.ru&lt;/code&gt; file with the following:&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;# config.ru&lt;/span&gt;

&lt;span class="nb"&gt;require_relative&lt;/span&gt; &lt;span class="s1"&gt;'./app'&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ShowExceptions&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Logger&lt;/span&gt;

  &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="s2"&gt;"/welcome"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Lint&lt;/span&gt;
    &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="no"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This DSL tells Rack to use the &lt;code&gt;Rack::ShowExceptions&lt;/code&gt; and &lt;code&gt;Rack::Logger&lt;/code&gt; middleware before using our &lt;code&gt;App&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Finally, add a new file &lt;code&gt;app.rb&lt;/code&gt; in the same directory with the following code:&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.rb&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="s1"&gt;'Content-Type'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'text/html'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'&amp;lt;h1&amp;gt;Greetings from Rack!!&amp;lt;/h1&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now. run the &lt;code&gt;rackup&lt;/code&gt; command, and navigate to &lt;code&gt;/welcome&lt;/code&gt; page on the server. You should be greeted with the response from our application.&lt;/p&gt;

&lt;p&gt;When a request comes from the web server, Rack will first use the exception-handling middleware, then the logging middleware, and finally run the app. The app will process the incoming request, and return the response, which will again be examined and processed by the middleware stack, this time in the reverse sequence. &lt;/p&gt;




&lt;p&gt;To summarize what we've learned so far, the Rack protocol powers the web in the Ruby world. It consists of two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Rack specification provides a simple interface that allows web servers and applications to talk to each other. &lt;/li&gt;
&lt;li&gt;The Rack gem provides tools that let us iteratively compose our Rack-compliant applications. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Additionally, Rack middleware components allow you to separate the application logic from peripheral concerns like authentication, logging, and error-handling. You can use any middleware built by the community for everyday use cases. &lt;/p&gt;

&lt;p&gt;That's it. I hope that now you have a much better understanding of Rack and the surrounding ecosystem. &lt;/p&gt;

&lt;p&gt;If you liked this post, you can find similar posts on my &lt;a href="https://akshaykhot.com/" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. If you have any questions or need further clarification, please let me know in the comments below. I look forward to your feedback. &lt;/p&gt;

&lt;p&gt;Thanks for reading, &lt;br&gt;
Akshay&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>rack</category>
    </item>
    <item>
      <title>Why You Need Strong Parameters in Rails</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Wed, 18 May 2022 16:57:57 +0000</pubDate>
      <link>https://dev.to/software_writer/why-you-need-strong-parameters-in-rails-10af</link>
      <guid>https://dev.to/software_writer/why-you-need-strong-parameters-in-rails-10af</guid>
      <description>&lt;p&gt;You've used the strong parameters in your Rails applications, but &lt;strong&gt;did you know what problem they are solving?&lt;/strong&gt; I didn't. So did some reading and learned about a common security vulnerability. In this post, I will explain the Mass Assignment vulnerability and how you can use the Rails strong parameters API to address it. &lt;/p&gt;

&lt;h3&gt;
  
  
  Mass Assignment Vulnerability
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What is it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Mass Assignment means assigning values to multiple variable or object properties at a time. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is it bad?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider the following object:&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Jason"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;company: &lt;/span&gt;&lt;span class="s2"&gt;"37signals"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's say we want to update the company's name to "Basecamp". The typical web-application way to do this would be to receive some data, preferably from a form submission from the user, and use that data to update our user's properties.&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;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;# some data&lt;/span&gt;
  &lt;span class="ss"&gt;company: &lt;/span&gt;&lt;span class="s2"&gt;"Basecamp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;# some data&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update!&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boom! You are done. But wait, the next day, Jason comes to the office and realizes there's a new owner. He's not listed as the owner anymore of the company. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How did that happen?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The answer is the other properties in the &lt;code&gt;params&lt;/code&gt; object above that I didn't list. Here's what the complete object looks like:&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;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Jason"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;company: &lt;/span&gt;&lt;span class="s2"&gt;"Basecamp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the software updated all the properties of the &lt;code&gt;user&lt;/code&gt; using the &lt;code&gt;params&lt;/code&gt; hash in its entirety, the &lt;code&gt;owner&lt;/code&gt; property was updated to &lt;code&gt;false&lt;/code&gt;. As a result, Jason is not an owner anymore. &lt;/p&gt;

&lt;p&gt;Now, of course, this is a very simple example to illustrate the problem. Of course, the web application is not foolish enough to ask whether the owner is indeed the owner on a front-end form that anyone can submit, and thankfully, Jason is still the owner. &lt;/p&gt;

&lt;p&gt;But you get the idea. &lt;strong&gt;It's terrible to update the properties of an object from a source that we don't trust!&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Mass assignment vulnerability occurs when your application assigns the data from a user input to multiple objects, variables, of database fields at once. Updating the object's properties allows an attacker to modify or overwrite the existing data. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the comments, Jeremy gave a scary real-world example. In 2012 &lt;a href="https://www.zdnet.com/home-and-office/networking/github-suspends-member-over-mass-assignment-hack/"&gt;GitHub was compromized&lt;/a&gt; by this vulnerability. A GitHub user used mass assignment that gave him administrator privileges to none other than the Ruby on Rails project. As GitHub co-founder, Tom Preston-Werner later said,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The root cause of the vulnerability was a failure to properly check incoming form parameters, a problem known as the mass-assignment vulnerability," &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;How to prevent it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution is simple. Before you update any object, filter out only the properties you want and nothing else!&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;class&lt;/span&gt; &lt;span class="nc"&gt;UsersController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
    &lt;span class="no"&gt;User&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="n"&gt;user_params&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;def&lt;/span&gt; &lt;span class="nf"&gt;update&lt;/span&gt;
    &lt;span class="no"&gt;User&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="nf"&gt;update_attributes!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="kp"&gt;private&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;user_params&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;:user&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:company&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pattern was so common that Rails released a plugin as well as a gem for it, and later versions of Rails (v4 onwards) provide it out of the box. Here's how it works:&lt;/p&gt;

&lt;h2&gt;
  
  
  Strong Parameters
&lt;/h2&gt;

&lt;p&gt;The philosophy behind strong parameters is &lt;em&gt;"&lt;strong&gt;assume unsafe until proven otherwise&lt;/strong&gt;"&lt;/em&gt;. In simple terms, Rails marks the parameters forbidden to be used in mass assignment until you explicitly mark them as safe. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do you mark parameters as safe?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;permit&lt;/code&gt; methods on the &lt;code&gt;params&lt;/code&gt; hash, which is an instance of &lt;code&gt;ActionController::Parameters&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;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:client&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:company&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we explicitly mark the &lt;code&gt;client&lt;/code&gt; parameter as required using the &lt;code&gt;require&lt;/code&gt; method, and only permit the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;company&lt;/code&gt; parameters inside the &lt;code&gt;client&lt;/code&gt;. If the &lt;code&gt;client&lt;/code&gt; parameter has an &lt;code&gt;admin&lt;/code&gt; parameter, Rails won't allow you to use it in a mass assignment operation. &lt;/p&gt;

&lt;p&gt;Let's open the Rails console by running &lt;code&gt;bin/rails console&lt;/code&gt; and run some experiments:&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;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="ss"&gt;client: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Jason"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;company: &lt;/span&gt;&lt;span class="s2"&gt;"Basecamp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;admin: &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;user: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"David"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;ActionController::Parameters {"client"=&amp;gt;{"name"=&amp;gt;"Jason", ..}} permitted: false&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Yes, you can simply create a &lt;code&gt;params&lt;/code&gt; object on fly. You don't need to make a request from the browser to access it. Pretty cool, right?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Notice that the output of the above code was an object with the &lt;code&gt;permitted&lt;/code&gt; property set to &lt;code&gt;false&lt;/code&gt;. Now, let's mark the &lt;code&gt;client&lt;/code&gt; parameter as required, only permitting the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;company&lt;/code&gt; parameters.&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;client_params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:client&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:company&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;ActionController::Parameters {"name"=&amp;gt;"Jason", "company"=&amp;gt;"Basecamp"} permitted: true&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that after validating the parameters and allowing only the attributes we want, the result &lt;code&gt;client_params&lt;/code&gt; has the &lt;code&gt;permitted&lt;/code&gt; property set to &lt;code&gt;true&lt;/code&gt;. The &lt;code&gt;client_params&lt;/code&gt; parameter is now safe to use in a mass-assignment operation.&lt;/p&gt;

&lt;p&gt;To permit an entire hash of parameters, use the &lt;code&gt;permit!&lt;/code&gt; method.&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;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:client&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;permit!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember that the strong parameters are only validated when you try to use them to update the active model's properties. It won't prevent you from accessing the data.&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;gt;&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;ActionController::Parameters {"client"=&amp;gt;#&amp;lt;ActionController::Parameters {"name"=&amp;gt;"Jason", "company"=&amp;gt;"Basecamp", "admin"=&amp;gt;true} permitted: false&amp;gt;, "user"=&amp;gt;{"name"=&amp;gt;"David"}} permitted: false&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;# This works&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;:user&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;ActionController::Parameters {"name"=&amp;gt;"David"} permitted: false&amp;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;:user&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"David"&lt;/span&gt;

&lt;span class="c1"&gt;# This doesn't&lt;/span&gt;
&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update!&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;:user&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Permit All Parameters
&lt;/h3&gt;

&lt;p&gt;If you want to permit all parameters by default, Rails provides the &lt;code&gt;permit_all_parameters&lt;/code&gt; option, which is &lt;code&gt;false&lt;/code&gt; by default. If set to &lt;code&gt;true&lt;/code&gt;, it will permit all the parameters (not recommended).&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="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;ActionController::Parameters {} permitted: false&amp;gt;&lt;/span&gt;

&lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permit_all_parameters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;

&lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;ActionController::Parameters {} permitted: true&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Take Specific Action for Unpermitted Parameters
&lt;/h3&gt;

&lt;p&gt;You can also control the behavior when Rails finds the unpermitted parameters. For this, set the &lt;code&gt;action_on_unpermitted_parameters&lt;/code&gt; property, which can take one of three values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;false&lt;/code&gt; to do nothing and continue as if nothing happened. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:log&lt;/code&gt; to log an event at the &lt;code&gt;DEBUG&lt;/code&gt; level.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:raise&lt;/code&gt; to raise an &lt;code&gt;ActionController::UnpermittedParameters&lt;/code&gt; exception.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"DHH"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;### false ###&lt;/span&gt;
&lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;action_on_unpermitted_parameters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:rails&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;ActionController::Parameters {} permitted: true&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;### log ###&lt;/span&gt;
&lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;action_on_unpermitted_parameters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:log&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:rails&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;2022&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mo"&gt;05&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;39&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;23.099411&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;56269&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4120&lt;/span&gt; &lt;span class="n"&gt;subscriber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;149&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="no"&gt;Unpermitted&lt;/span&gt; &lt;span class="ss"&gt;parameter: &lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#&amp;lt;ActionController::Parameters {} permitted: true&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;### raise ###&lt;/span&gt;
&lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;action_on_unpermitted_parameters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:raise&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:rails&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#../metal/strong_parameters.rb:1002:in `unpermitted_parameters!': found unpermitted parameter: :name (ActionController::UnpermittedParameters)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;I hope you now have a better understanding of why strong parameters exist in Rails and how you can use them to build safe and secure applications. If you found this article useful, or have any questions, please let me know. &lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Understanding Rails Parameters</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Tue, 17 May 2022 14:45:19 +0000</pubDate>
      <link>https://dev.to/software_writer/understanding-rails-parameters-3npa</link>
      <guid>https://dev.to/software_writer/understanding-rails-parameters-3npa</guid>
      <description>&lt;p&gt;If you are building a dynamic web application, you will need to access the user-submitted data on the server. Maybe the user uploaded a photo, wrote a comment, posted a tweet, or clicked a link that includes the &lt;code&gt;id&lt;/code&gt; of the article they want to read. &lt;strong&gt;How do you access this data on the back-end?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;That's where &lt;code&gt;parameters&lt;/code&gt; enter into the picture. This article explains everything you need to know to understand Rails parameters. &lt;/p&gt;

&lt;p&gt;There are two ways to get data from the front-end to the back-end. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Query Strings&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;Includes the data provided in the URL after the &lt;code&gt;?&lt;/code&gt;, e.g. &lt;code&gt;blog.com/posts?id=3&amp;amp;user=ak&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Data consists of key-value pairs separated by &lt;code&gt;&amp;amp;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Typically submitted in a GET request, e.g. clicking a link &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Form Submission&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;Contains the data entered into a form by the user. &lt;/li&gt;
&lt;li&gt;Typically posted in a POST request, e.g. submitting a form&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The good thing is that Rails doesn't care about the request type (&lt;code&gt;GET&lt;/code&gt; or &lt;code&gt;POST&lt;/code&gt;) when accessing the data on the server. It won't distinguish between the information from query strings and the data from a form submission. It groups all the data into the &lt;code&gt;params&lt;/code&gt; hash, which is available in the controller. &lt;/p&gt;

&lt;h2&gt;
  
  
  Query Strings
&lt;/h2&gt;

&lt;p&gt;The following route in the &lt;code&gt;routes.rb&lt;/code&gt; file routes the &lt;code&gt;/posts/sample&lt;/code&gt; URL to the &lt;code&gt;sample&lt;/code&gt; method on the &lt;code&gt;PostsController&lt;/code&gt; class.&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;get&lt;/span&gt; &lt;span class="s1"&gt;'posts/sample'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'posts#sample'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the &lt;code&gt;PostsController#sample&lt;/code&gt; method. To keep things simple, I am rendering plain text without using a view template.&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;class&lt;/span&gt; &lt;span class="nc"&gt;PostsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sample&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;plain: &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;id: &lt;/span&gt;&lt;span class="si"&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="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;user: &lt;/span&gt;&lt;span class="si"&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;:user&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&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;Now, when I go to the URL &lt;code&gt;http://localhost:3000/posts/sample?id=3&amp;amp;user=ak&lt;/code&gt;, it renders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"id"=&amp;gt;"3", "user"=&amp;gt;"ak", "controller"=&amp;gt;"posts", "action"=&amp;gt;"sample"} 

id: 3 
user: ak
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the &lt;code&gt;params&lt;/code&gt; hash also includes the controller and action names. Though you can access it in your controller, Rails recommends using the &lt;a href="https://api.rubyonrails.org/v7.0.3/classes/ActionController/Metal.html#method-i-controller_name"&gt;&lt;code&gt;controller_name&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://api.rubyonrails.org/v7.0.3/classes/AbstractController/Base.html#method-i-action_name"&gt;&lt;code&gt;action_name&lt;/code&gt;&lt;/a&gt; methods to access these values. &lt;/p&gt;

&lt;h2&gt;
  
  
  Form Submission
&lt;/h2&gt;

&lt;p&gt;The following route in the &lt;code&gt;routes.rb&lt;/code&gt; file routes the&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;posts/add&lt;/code&gt; URL to the &lt;code&gt;add&lt;/code&gt; method on the &lt;code&gt;PostsController&lt;/code&gt; class.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;posts/upload&lt;/code&gt; URL to the &lt;code&gt;build&lt;/code&gt; method on the &lt;code&gt;PostsController&lt;/code&gt; class.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'posts/add'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'posts#add'&lt;/span&gt;
&lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="s1"&gt;'posts/upload'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'posts#build'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the &lt;code&gt;PostsController#add&lt;/code&gt; method, which renders the &lt;code&gt;add.html.erb&lt;/code&gt; view. With the Rails conventions, it will render the &lt;code&gt;add.html.erb&lt;/code&gt; view template.&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;class&lt;/span&gt; &lt;span class="nc"&gt;PostsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&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;Let's create a simple form in our &lt;code&gt;add.html.erb&lt;/code&gt; template. When submitted, this form will make a &lt;code&gt;POST&lt;/code&gt; request on the &lt;code&gt;/posts/upload&lt;/code&gt; endpoint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&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;"upload"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"post"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Name:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"user_name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"mail"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;E-mail:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"mail"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"user_email"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&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;p&gt;To handle this &lt;code&gt;Post&lt;/code&gt; request, let's add the &lt;code&gt;build&lt;/code&gt; method on the &lt;code&gt;PostsController&lt;/code&gt;. Again, I am simply rendering the data as plain text, without the view to keep things simple. I have also disabled the Rails &lt;code&gt;authenticity_token&lt;/code&gt; security measure to avoid adding unnecessary complexity to the example (don't do this in a real application).&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;skip_before_action&lt;/span&gt; &lt;span class="ss"&gt;:verify_authenticity_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: :build&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;build&lt;/span&gt;
  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;plain: &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;name: &lt;/span&gt;&lt;span class="si"&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;:user_name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;email: &lt;/span&gt;&lt;span class="si"&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;:user_email&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when you enter some data in the form and click the &lt;strong&gt;Submit&lt;/strong&gt; button, you are greeted with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"user_name"=&amp;gt;"Akshay", "user_email"=&amp;gt;"ak@email.com", "controller"=&amp;gt;"posts", "action"=&amp;gt;"build"} 

name: Akshay 
email: ak@email.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the keys I am using on the &lt;code&gt;params&lt;/code&gt; hash are the same values I used for the &lt;code&gt;name&lt;/code&gt; attributes on the form. Rails extracts these values from the submitted data and adds them to the &lt;code&gt;params&lt;/code&gt; hash. &lt;/p&gt;

&lt;p&gt;Instead of sending the &lt;code&gt;name&lt;/code&gt; values as keys directly, you can group them under a common key in the &lt;code&gt;name&lt;/code&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&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;"upload"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"post"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Name:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"client[user_name]"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"mail"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;E-mail:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"mail"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"client[user_email]"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&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;p&gt;Let's modify the &lt;code&gt;build&lt;/code&gt; method to access the nested parameters:&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;build&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;plain: &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_json&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;name: &lt;/span&gt;&lt;span class="si"&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;:client&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:user_name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;email: &lt;/span&gt;&lt;span class="si"&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;:client&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:user_email&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&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 prints the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"client":{"user_name":"Akshay","user_email":"ak@email.com"},"controller":"posts","action":"build"} 

name: Akshay 
email: ak@email.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that Rails has grouped the &lt;code&gt;user_name&lt;/code&gt; and &lt;code&gt;user_email&lt;/code&gt; under the &lt;code&gt;client&lt;/code&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Sending JSON
&lt;/h2&gt;

&lt;p&gt;For a rails API, you can send JSON data from the client. Rails will load it into the &lt;code&gt;params&lt;/code&gt; hash, and you can access it in the controllers. For example, if you send the following JSON data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"company"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"acme"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"address"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"123 Carrot Street"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;params&lt;/code&gt; hash will be&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="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:company&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"acme"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"address"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"123 Carrot Street"&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;h2&gt;
  
  
  Dynamic Parameters
&lt;/h2&gt;

&lt;p&gt;If your route URL contains a symbol, Rails will use that symbol name as the key in the &lt;code&gt;params&lt;/code&gt; hash. In the following example, the route instructs Rails to map the URL parameter to &lt;code&gt;title&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="c1"&gt;# routes.rb&lt;/span&gt;
&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'posts/lookup/:title'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'posts#lookup'&lt;/span&gt;

&lt;span class="c1"&gt;# posts_controller&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lookup&lt;/span&gt;
  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;plain: &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;title: &lt;/span&gt;&lt;span class="si"&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;:title&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# browser output&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"controller"&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"action"&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"lookup"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"rails"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 

&lt;span class="ss"&gt;title: &lt;/span&gt;&lt;span class="n"&gt;rails&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Passing Parameters when Redirecting
&lt;/h2&gt;

&lt;p&gt;When redirecting to another route, you can pass the parameters by passing them as hash parameters to the &lt;code&gt;redirect_to&lt;/code&gt; method. For example,&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;redirect_to&lt;/span&gt; &lt;span class="ss"&gt;controller: &lt;/span&gt;&lt;span class="s1"&gt;'articles'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;action: &lt;/span&gt;&lt;span class="s1"&gt;'show'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;user: &lt;/span&gt;&lt;span class="s1"&gt;'ak'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ArticlesController&lt;/code&gt; can access the passed values using the &lt;code&gt;params&lt;/code&gt; hash as usual. However, an important thing to note is that you can't redirect a &lt;code&gt;POST&lt;/code&gt; request. &lt;/p&gt;




&lt;p&gt;I hope that you have a pretty good understanding of how parameters work in Rails by now. To summarize what we've learned so far:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rails doesn't differentiate between the data that comes from a GET request (query string parameters) vs. the data that comes from a POST request (form submission), and &lt;/li&gt;
&lt;li&gt;All the data is available in the &lt;code&gt;params&lt;/code&gt; hash. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next post, we will look at a common problem in software, known as &lt;strong&gt;Mass Assignment Vulnerability&lt;/strong&gt;, and how Rails helps us tackle it by providing strong parameters. &lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Useful Linux Commands</title>
      <dc:creator>Akshay Khot</dc:creator>
      <pubDate>Fri, 13 May 2022 23:57:20 +0000</pubDate>
      <link>https://dev.to/software_writer/useful-linux-commands-2acn</link>
      <guid>https://dev.to/software_writer/useful-linux-commands-2acn</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Learning the Linux command line, like becoming an accomplished pianist, is not something that we pick up in an afternoon. It takes years of practice.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The Linux Command Line, William E. Shotts&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since I switched to Ruby on Rails from .NET, I find myself increasingly using the terminal every day. One thing I've realized as a Rails developer is that you need to have some basic competence with the terminal, as &lt;strong&gt;you will use it all the time&lt;/strong&gt;. So I spent some time getting familiar with some basic Linux commands, and this post tries to summarize the essentials. &lt;/p&gt;

&lt;p&gt;This is not a comprehensive list, but I will try to keep adding to this list on the &lt;a href="https://akshaykhot.com/linux-useful-commands/" rel="noopener noreferrer"&gt;original blog post&lt;/a&gt; as I learn more.  If you want a detailed overview of Linux operating system, I highly recommend &lt;a href="https://learning.oreilly.com/library/view/the-linux-command/9781492071235/" rel="noopener noreferrer"&gt;The Linux Command Line, 2nd Edition&lt;/a&gt; by William E. Shotts. &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%2Foaon62ip2jofgiz4mnd7.jpeg" 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%2Foaon62ip2jofgiz4mnd7.jpeg" alt="The Linux Command Line, 2nd Edition"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So here are some of the commands that you might find useful as a developer on a Linux/Mac machine. &lt;/p&gt;

&lt;h3&gt;
  
  
  File system
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;cat&lt;/code&gt; displays the contents of a file, or concatenates the contents of multiple files. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;touch&lt;/code&gt; creates a file if it doesn't exist; updates the timestamp if it exists. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;grep&lt;/code&gt; searches the term &lt;code&gt;client&lt;/code&gt; in the provided file. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-i&lt;/code&gt; for case-insensitive search&lt;/li&gt;
&lt;li&gt; &lt;code&gt;-n&lt;/code&gt; for printing line numbers next to the results.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep &lt;/span&gt;client &lt;span class="nt"&gt;-in&lt;/span&gt; /etc/ssh/sshd_config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;less&lt;/code&gt; displays a large file one page at a time. Use the space bar to go forward and &lt;code&gt;b&lt;/code&gt; to go back. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;file&lt;/code&gt; tells the format of a file. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;head/tail&lt;/code&gt; displays the top or bottom of the file. Pass &lt;code&gt;-n&lt;/code&gt; for number of lines, &lt;code&gt;head -5 file&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;reset&lt;/code&gt; re-initializes terminal. Especially useful after resizing the window or if a command results in scrambled window. &lt;/p&gt;

&lt;h3&gt;
  
  
  I/O Redirection
&lt;/h3&gt;

&lt;p&gt;To send the output of a command to a file, use &lt;code&gt;&amp;gt;&lt;/code&gt;, which overwrites the existing content of the file. To append, use &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To send the output of a command to the standard input of another command, use the pipe &lt;code&gt;|&lt;/code&gt; character.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;head&lt;/span&gt; /proc/cpuinfo | &lt;span class="nb"&gt;tr &lt;/span&gt;a-z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sort&lt;/code&gt; sorts lines of text.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;uniq&lt;/code&gt; Report or omit repeated lines&lt;/p&gt;

&lt;p&gt;&lt;code&gt;grep&lt;/code&gt; Print lines matching a pattern&lt;/p&gt;

&lt;p&gt;&lt;code&gt;wc&lt;/code&gt; Print newline, word, and byte counts for each file&lt;/p&gt;

&lt;p&gt;&lt;code&gt;head&lt;/code&gt; Output the first part of a file&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tail&lt;/code&gt; Output the last part of a file&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tee&lt;/code&gt; Read from standard input and write to standard output and files&lt;/p&gt;

&lt;h3&gt;
  
  
  Processes
&lt;/h3&gt;

&lt;p&gt;A process is a running program. Each process has a process ID (PID). Sometimes a computer will become sluggish or an application will stop responding. &lt;/p&gt;

&lt;p&gt;Here are some of the tools available at the command line that let us examine what programs are doing.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ps&lt;/code&gt; lists all the running processes. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;ps x&lt;/code&gt; Show all of your running processes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ps ax&lt;/code&gt; Show all processes on the system, not just the ones you own.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ps u&lt;/code&gt; Include more detailed information on processes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ps w&lt;/code&gt; Show full command names, not just what fits on one line.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To check on a specific process, add the PID at the end of the &lt;code&gt;ps&lt;/code&gt; command, e.g. &lt;code&gt;ps u 1234&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;top&lt;/code&gt; displays tasks&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jobs&lt;/code&gt; lists active jobs&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bg&lt;/code&gt; places a job in the background&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fg&lt;/code&gt; places a job in the foreground&lt;/p&gt;

&lt;p&gt;&lt;code&gt;kill&lt;/code&gt; sends a signal to a process&lt;/p&gt;

&lt;p&gt;&lt;code&gt;killall&lt;/code&gt; kills processes by name&lt;/p&gt;

&lt;p&gt;&lt;code&gt;shutdown&lt;/code&gt; shuts down or reboots the system&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Background Process&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Normally, after you run a command, you don't get the prompt back until the process finishes. You can detach a process from the shell with the &lt;code&gt;&amp;amp;&lt;/code&gt; which runs it as a background process. The shell returns immediately with the PID of the process. &lt;/p&gt;

&lt;p&gt;If you want to keep a program running when you log out from a remote machine, use the &lt;a href="https://linux.die.net/man/1/nohup" rel="noopener noreferrer"&gt;nohup&lt;/a&gt; command. &lt;/p&gt;

&lt;h3&gt;
  
  
  File Modes &amp;amp; Permissions
&lt;/h3&gt;

&lt;p&gt;Determine if a user can read, write, or run the file. View the permissions using &lt;code&gt;ls -l&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;-rw-rw-r--&lt;/span&gt; 1 ak ak  14 Oct  5 07:00 file_one
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Leftmost character:  &lt;code&gt;-&lt;/code&gt; indicates a file, &lt;code&gt;d&lt;/code&gt; indicates a directory. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;rwx&lt;/code&gt; stands for read, write, and execute. From left to right, the permissions stand for a user, group, and other. &lt;/p&gt;

&lt;p&gt;To modify the permissions, use the &lt;code&gt;chmod&lt;/code&gt; command, e.g. &lt;code&gt;chmod 644 file&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mode&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;Used For&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;644&lt;/td&gt;
&lt;td&gt;user: r/w; group, other: read&lt;/td&gt;
&lt;td&gt;files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;600&lt;/td&gt;
&lt;td&gt;user: read/write; group, other: none&lt;/td&gt;
&lt;td&gt;files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;755&lt;/td&gt;
&lt;td&gt;user: read/write/execute; group, other: read/execute&lt;/td&gt;
&lt;td&gt;dirs, programs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;700&lt;/td&gt;
&lt;td&gt;user: read/write/execute; group, other: none&lt;/td&gt;
&lt;td&gt;dirs, programs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;711&lt;/td&gt;
&lt;td&gt;user: read/write/execute; group, other: execute&lt;/td&gt;
&lt;td&gt;dirs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Security
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;id&lt;/code&gt; displays user identity&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chmod&lt;/code&gt; changes a file’s mode&lt;/p&gt;

&lt;p&gt;&lt;code&gt;su&lt;/code&gt; runs a shell as another user&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo&lt;/code&gt; executes a command as another user&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chown&lt;/code&gt; changes a file’s owner&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chgrp&lt;/code&gt; changes a file’s group ownership&lt;/p&gt;

&lt;p&gt;&lt;code&gt;passwd&lt;/code&gt; changes a user’s password&lt;/p&gt;

&lt;h3&gt;
  
  
  Environment Variables
&lt;/h3&gt;

&lt;p&gt;The Linux shell maintains information about the current environment. Programs use this data to change their runtime behavior, e.g. selecting different database when the application is running in the production environment, as opposed to the test environment. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;printenv&lt;/code&gt; prints part or all of the environment&lt;/p&gt;

&lt;p&gt;&lt;code&gt;set&lt;/code&gt; sets shell options (show the environment when used without argument)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export&lt;/code&gt; exports environment to subsequently executed programs&lt;/p&gt;

&lt;p&gt;&lt;code&gt;alias&lt;/code&gt; creates an alias for a command (or show all aliases when used without argument)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set environment variable&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;akshay
&lt;span class="nb"&gt;export &lt;/span&gt;NAME
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sets it locally. For setting it globally, add it in the &lt;code&gt;~/.bashrc&lt;/code&gt; or &lt;code&gt;~/.zshconfig&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;Hope that helps. &lt;/p&gt;

</description>
      <category>linux</category>
      <category>terminal</category>
    </item>
  </channel>
</rss>
