Skip to content

Commit

Permalink
use tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ka8725 committed Dec 19, 2023
1 parent 3d790f3 commit f4aeb0c
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions _posts/2023-12-19-smart-route-aliases-in-rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ title: "Smart route aliases in Rails"
headline: "Smart routes aliases in Rails"
modified: 2023-12-19 18:00:50 +0100
description: "Learn how to define smart aliases in Rails router."
tags: [rails]
tags: [rails, rails-development]
featured_post: false
image: route-66.jpg
---

For search engine optimization and marketing purposes, a Rails project may require the definition of an alias route. This can be challenging if the original route contains a dynamic component that depends on the current user's session.
Sometimes, for SEO and marketing reasons, you might need to create an alias route in your Rails project. This can get tricky if the original route relies on dynamic elements that change based on the user's session.

In our project, we sought to create an easy shorthand `/edit_my_plans` for the user-specific route `/users/:id/edit?tab=plans`.
In our project, we wanted to create a quick and easy shortcut, `/edit_my_plans`, for the user-specific route `/users/:id/edit?tab=plans`.

For instance, Rails provides a mechanism to define this type of redirect using the following syntax (the code example is extracted from their official documentation):
For example, Rails offers a way to set up this type of redirect using the following code (taken from their official documentation):

```ruby
get '/stories/:name', to: redirect { |path_params, req| "/articles/#{path_params[:name].pluralize}" }
```

Employing this knowledge, we devised a clean and legible approach to defining this type of route, incorporating business logic of any complexity directly within the router:
With this knowledge in hand, we developed a clear and straightforward method for defining these types of routes, allowing us to embed business logic of any level of complexity directly into the router:

```ruby
# in config/routes.rb:
get "/edit_my_plans", to: redirect(RoutesAlias.edit_my_plans)
```

And this is the dedicated service class, aptly named `RoutesAlias`, which encapsulates the intricate business logic. It's more judicious to isolate such intricate operations into a separate class, rather than embedding them within the router definition. We've placed it in the `app/lib` directory, though in your specific project, you may choose a different location:
This is our special class, called `RoutesAlias`, that handles all the complex stuff. It's better to keep this stuff separate from the router, so we put it in the `app/lib` folder. You can put it somewhere else in your project if you want.

```ruby
class RoutesAlias
Expand Down Expand Up @@ -57,6 +57,6 @@ class RoutesAlias
end
```

Now, marketing sites, such as landing pages or others, can effortlessly utilize the generic URL `/edit_my_plans`. Rails will orchestrate the redirection to the appropriate route, whether it's a login form or a specific homepage tailored for distinct user types.
Now, marketing sites, like landing pages or other marketing pages, can easily use the generic URL `/edit_my_plans`. Rails will handle everything, taking you to the right place, whether it's a login form, a specific homepage for a particular user type, or something else.

I hope this assists in your Rails project. Happy coding!
Hope this helps with your Rails project! Happy coding!

0 comments on commit f4aeb0c

Please sign in to comment.