Skip to content

Commit

Permalink
smart route aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ka8725 committed Dec 19, 2023
1 parent 3da1ca3 commit aeef3e0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions _posts/2023-12-19-smart-route-aliases-in-rails.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
layout: post
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]
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.

In our project, we sought to create an easy shorthand `/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):

```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:

```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:

```ruby
class RoutesAlias
include Rails.application.routes.url_helpers

attr_reader :session

def self.edit_my_plans
proc { |_params, request| new(request.session).edit_my_plans }
end

def initialize(session)
@session = session
end

def edit_my_plans
current_user = Authentication.user_from_session(session)

case current_user&.user_type
when "vendor"
edit_user_path(current_user, tab: "plans")
when "user"
projects_path
when nil
"/auth/auth0?origin=/edit_my_plans"
end
end
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.

I hope this assists in your Rails project. Happy coding!
Binary file added images/route-66.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit aeef3e0

Please sign in to comment.