<?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: LUIS CUEVAS</title>
    <description>The latest articles on DEV Community by LUIS CUEVAS (@lcuevastodoit).</description>
    <link>https://dev.to/lcuevastodoit</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%2F995399%2F9c41add4-c960-4160-b0eb-c4872efddddc.png</url>
      <title>DEV Community: LUIS CUEVAS</title>
      <link>https://dev.to/lcuevastodoit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lcuevastodoit"/>
    <language>en</language>
    <item>
      <title>Rails Routes - Mastering Routing in Ruby on Rails: A Concise Guide with Code Examples</title>
      <dc:creator>LUIS CUEVAS</dc:creator>
      <pubDate>Thu, 07 Sep 2023 01:31:27 +0000</pubDate>
      <link>https://dev.to/lcuevastodoit/rails-routes-mastering-routing-in-ruby-on-rails-a-concise-guide-with-code-examples-g3</link>
      <guid>https://dev.to/lcuevastodoit/rails-routes-mastering-routing-in-ruby-on-rails-a-concise-guide-with-code-examples-g3</guid>
      <description>&lt;h2&gt;
  
  
  1. Single Route Resources
&lt;/h2&gt;

&lt;p&gt;Single route resources are a straightforward way to define routes for a resource with only one route. Let's create a route for a "book" resource using Rails resources 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="c1"&gt;# config/routes.rb&lt;/span&gt;
&lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:show&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we've defined a single "show" route for the "books" resource, which maps to the "BooksController#show" action.&lt;br&gt;
But if we want all the whole REST routes for books, we use 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;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Nested Routes
&lt;/h2&gt;

&lt;p&gt;Nested routes in Ruby on Rails involve physically nesting one &lt;code&gt;resource&lt;/code&gt; inside a block supplied to another &lt;code&gt;resource&lt;/code&gt; in the routes file. This allows for URLs like '/books/2/chapters/10', where chapters are nested under books.&lt;/p&gt;

&lt;p&gt;Nested routes allow you to express hierarchical relationships between resources. For instance, if you have a 'Chapter' resource that belongs to a 'Book,' you can set up nested routes 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;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:chapters&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;of course we have defined associations between models&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/models/book.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:chapters&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&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/models/chapter.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Chapter&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:book&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Member and Collection Routes
&lt;/h2&gt;

&lt;p&gt;In Ruby on Rails, when defining routes for a resource, you have the flexibility to add custom actions using member and collection routes. These two types of routes serve different purposes and are suitable for distinct scenarios.&lt;/p&gt;

&lt;p&gt;Suppose you have a "books" resource and want to add a custom "highlight" action for a specific book. You can define a member route 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;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;member&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'highlight'&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, you can access the "highlight" action for a specific book using a URL like "/books/1/highlight," where "1" represents the book's ID.&lt;/p&gt;

&lt;p&gt;Suppose you want to add a custom "search" action that searches for books across the entire collection. You can define a collection route 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;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;collection&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'search'&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, you can perform a search action that applies to all books using a URL like "/books/search."&lt;/p&gt;

&lt;p&gt;Note that &lt;strong&gt;Member routes&lt;/strong&gt; typically map to actions that perform operations on a single member or instance of a resource.&lt;br&gt;
&lt;strong&gt;Collection routes&lt;/strong&gt; map to actions that involve the entire collection of a resource.&lt;/p&gt;
&lt;h2&gt;
  
  
  4. Adding Non-RESTful Routes
&lt;/h2&gt;

&lt;p&gt;Sometimes, you may need routes that don't follow RESTful conventions. For example, let's create a custom route for marking a book as a favorite:&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/routes.rb&lt;/span&gt;
&lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="s1"&gt;'favorite'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;on: :member&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below are other different approaches to implement a non-RESTful route for this scenario:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Using the match Method&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This approach allows you to specify the HTTP method (e.g., POST) and the controller action to be invoked. The :as option allows you to create a named route helper method, such as favorite_book_path, for generating URLs to this route.&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;match&lt;/span&gt; &lt;span class="s1"&gt;'/books/:id/favorite'&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;'books#favorite'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;via: :post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as: :favorite_book&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Using post Method with on: Option&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;By specifying the on: option within the post method in your routes file. This approach explicitly states that the "favorite" route is a member route, and it maps to the favorite action in the BooksController&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;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="s1"&gt;'favorite'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;on: :member&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;'books#favorite'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Using Custom Named Routes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;With this approach, you have flexibility in naming the route and can use the generated named route helper (e.g., mark_favorite_path or custom_favorite_route_path) to generate URLs for this non-RESTful action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="s1"&gt;'/books/:id/favorite'&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;'books#favorite'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as: :mark_favorite&lt;/span&gt;

&lt;span class="c1"&gt;# OR, using a custom helper method name&lt;/span&gt;
&lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="s1"&gt;'/books/:id/favorite'&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;'books#favorite'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as: :custom_favorite_route&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now, you can mark a book as a favorite by making a POST request to "/books/1/favorite."&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Redirects
&lt;/h2&gt;

&lt;p&gt;In Rails, you can define redirects easily in your routes file. Suppose you want to redirect "/old_path" to "/new_path":&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/routes.rb&lt;/span&gt;
&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/old_path'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="n"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/new_path'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will redirect any request to "/old_path" to "/new_path."&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Wildcard Routes
&lt;/h2&gt;

&lt;p&gt;Wildcard routes are a powerful tool for handling dynamic URLs. Suppose you want to capture all requests to "/books/*" and route them to a specific controller action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/books/*path'&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;'books#show'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this wildcard route, you can capture various book-related paths and route them to the "BooksController#show" action.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Some Anti-Patterns, or Please Dont
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Overuse of Custom Routes
&lt;/h3&gt;

&lt;p&gt;Anti-Pattern: Defining excessive custom routes for every controller action&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'custom_action_1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;on: :member&lt;/span&gt;
  &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="s1"&gt;'custom_action_2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;on: :collection&lt;/span&gt;
  &lt;span class="n"&gt;put&lt;/span&gt; &lt;span class="s1"&gt;'custom_action_3'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;on: :member&lt;/span&gt;
  &lt;span class="c1"&gt;# ... more custom actions ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, it's recommended to use RESTful routes whenever possible and resort to custom routes only when necessary.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verbosity in Named Routes:
&lt;/h3&gt;

&lt;p&gt;Anti-Pattern: Using overly verbose names for named routes.&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;# BAD&lt;/span&gt;
&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/books/:id/edit_book'&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;'books#edit'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as: :edit_book_action&lt;/span&gt;

&lt;span class="c1"&gt;# GOOD&lt;/span&gt;
&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/books/:id/edit'&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;'books#edit'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as: :edit_book&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Unused or Dead Routes
&lt;/h3&gt;

&lt;p&gt;Anti-Pattern: Defining routes that are no longer used in the 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;get&lt;/span&gt; &lt;span class="s1"&gt;'/unused_route'&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;'unused#action'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Such routes should be removed to maintain a clean and maintainable route configuration&lt;/p&gt;

&lt;h3&gt;
  
  
  Complex Route Constraints
&lt;/h3&gt;

&lt;p&gt;Anti-Pattern: Overcomplicating routes with complex constraints.&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;'/books/:id'&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;'books#show'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;constraints: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="sr"&gt;/\d{3}/&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep route constraints simple and understandable, and use them sparingly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lack of RESTful Structure with RESTful actions
&lt;/h3&gt;

&lt;p&gt;Anti-Pattern: Creating routes that don't adhere to RESTful principles when these are for RESTful action in the first place.&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;# BAD because show is a RESTful action&lt;/span&gt;
&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/books/show_book/:id'&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;'books#show'&lt;/span&gt;
&lt;span class="c1"&gt;# Better&lt;/span&gt;
&lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:show&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Inconsistent Route Naming
&lt;/h3&gt;

&lt;p&gt;Anti-Pattern: Using inconsistent naming conventions for routes.&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;# Wrong&lt;/span&gt;
&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/books/show_book/:id'&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;'books#show'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as: :display_single_book&lt;/span&gt;
&lt;span class="c1"&gt;# Best&lt;/span&gt;
&lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:show&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consistency in naming conventions makes the codebase more understandable&lt;/p&gt;

&lt;p&gt;By avoiding these anti-patterns and following Rails' conventions for routing definitions, you can maintain a clean and maintainable routing configuration in your Ruby on Rails application, making it easier for developers to understand and work with the code.&lt;/p&gt;

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

&lt;p&gt;Mastering routing in Ruby on Rails is essential for building robust web applications. In this concise guide, we've covered single route resources, nested routes, member and collection routes, non-RESTful routes, redirects, and wildcard routes, and anti-patterns, each of these illustrated with code examples. By understanding these routing concepts, you'll be better equipped to handle complex routing scenarios in your Rails applications.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>routing</category>
    </item>
    <item>
      <title>Using GIVEN-WHEN-THEN on RSpec test classes</title>
      <dc:creator>LUIS CUEVAS</dc:creator>
      <pubDate>Sun, 11 Jun 2023 02:24:02 +0000</pubDate>
      <link>https://dev.to/lcuevastodoit/using-given-when-then-on-rspec-test-classes-19nk</link>
      <guid>https://dev.to/lcuevastodoit/using-given-when-then-on-rspec-test-classes-19nk</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fopenclipart.org%2Fimage%2F300px%2F173574" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fopenclipart.org%2Fimage%2F300px%2F173574" alt="A Ruby icon surprised by the article" width="300" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Given-When-Then in RSpec&lt;/strong&gt;&lt;br&gt;
RSpec provides a way to use Given-When-Then in test classes by using aliases. Aliases are a way to define new names for existing RSpec methods. By using aliases, developers can write tests that use Given-When-Then syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RSpec&lt;/strong&gt; is a testing tool for Ruby, created for behavior-driven development (BDD / TDD). It is the most frequently used testing library for Ruby in production applications. RSpec provides a powerful DSL (domain-specific language) that allows developers to write expressive and readable tests. Also there are a number of other popular testing libraries for Ruby, such as Test::Unit and Minitest.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;but always the lazy one in me wants something easier to read and remember when coding tests&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1h60u02qpzenx7zqadif.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1h60u02qpzenx7zqadif.jpg" alt="A comic character saying behold" width="665" height="375"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'spec_helper'&lt;/span&gt;

&lt;span class="no"&gt;RSpec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;testing&lt;/span&gt; &lt;span class="s1"&gt;'Calculator'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Given&lt;/span&gt; &lt;span class="s1"&gt;'a calculator'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:calculator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="no"&gt;Calculator&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="no"&gt;When&lt;/span&gt; &lt;span class="s1"&gt;'the calculator adds two numbers'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;before_that&lt;/span&gt; &lt;span class="k"&gt;do&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;calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;

      &lt;span class="no"&gt;Then&lt;/span&gt; &lt;span class="s1"&gt;'the result should be the sum of the two numbers'&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="vi"&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;5&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;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;was it easy to read?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the code example above, we define aliases for example_group and example elements using the keywords Given, When, and Then. We also define a helper method &lt;code&gt;before_that&lt;/code&gt; that is used as wrapper of the before(:each) method. Also we use 'testing' as an alias for 'describe'&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We have capitalized the words Given When Then so that they are not confused with Ruby reserved words that are the same but lowercase.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We then define a test class for a Calculator object. We use the &lt;code&gt;Given&lt;/code&gt; alias to define a context for the test, and the &lt;code&gt;When&lt;/code&gt; alias to define the behavior being tested. We use the &lt;code&gt;before_that&lt;/code&gt; helper method to set up the preconditions for the test. Finally, we use the &lt;code&gt;Then alias&lt;/code&gt; to define the expected outcome of the test.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Given-When-Then?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Given-When-Then&lt;/strong&gt; is a test description technique that helps developers to write tests that are easy to read and understand. The technique is based on three parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Given: This part describes the preconditions of the test. It sets up the context for the test.&lt;/li&gt;
&lt;li&gt; When: This part describes the action that is being tested. It specifies the behavior that is being tested.&lt;/li&gt;
&lt;li&gt; Then: This part describes the expected outcome of the test. It specifies the result that is expected from the behavior being tested.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to do use aliases on RSpec tests
&lt;/h2&gt;

&lt;p&gt;To enable this aliases just add these sentences on the spec_helper.rb:&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;configure&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;config&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
  &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alias_example_group_to&lt;/span&gt; &lt;span class="ss"&gt;:testing&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alias_example_group_to&lt;/span&gt; &lt;span class="ss"&gt;:Given&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alias_example_group_to&lt;/span&gt; &lt;span class="ss"&gt;:When&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alias_example_to&lt;/span&gt; &lt;span class="ss"&gt;:Then&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;before_that&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&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="n"&gt;block&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;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;Calculator&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int_a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;int_b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;int_a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;int_b&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;&lt;a href="https://media2.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%2Fxo7yi7cnqw74ip2089bz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxo7yi7cnqw74ip2089bz.png" alt="rspec test result" width="740" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;IMHO Given-When-Then is a powerful test description technique that helps developers to write tests that are easy to read and understand. RSpec provides aliases that allow developers to use Given-When-Then syntax in their tests. By using aliases on RSpec, developers can write tests that are expressive and readable, making it easier to maintain and debug their code.&lt;/p&gt;

&lt;p&gt;By following these dev tricks, you can write Given-When-Then tests that are easy to read and understand. This will make it easier to maintain, document and debug your code.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>rspec</category>
      <category>rails</category>
      <category>bdd</category>
    </item>
  </channel>
</rss>
