Basically, you can implement a concern inside app/controller/concerns
like this:
# app/controller/concerns/slug_redirector.rb
module SlugRedirector
extend ActiveSupport::Concern
included do
before_action :redirect_to_canonical_route
end
def redirect_to_canonical_route
record = get_record(model)
return if record.blank?
canonical_path = get_canonical_path(record)
redirect_to canonical_path, status: :moved_permanently if should_redirect?(canonical_path)
end
private
def model
controller_name.classify.constantize
end
def namespace
self.class.module_parent_name&.downcase&.to_sym
end
def get_record(model)
instance_variable_get("@#{model.name.underscore}")
end
def get_canonical_path(record)
polymorphic_path([namespace, record])
end
def should_redirect?(canonical_path)
request.path != canonical_path
end
end
Then include it into your controllers below the load and authorization call, like this:
class SpeakersController < ApplicationController
load_and_authorize_resource
include SlugRedirector
# GET /speakers
def index; end
# GET /speakers/1
def show; end
end
This concern implements a redirect to the latest slug using the FriendlyId gem.
The concern includes a before_action
that calls the redirect_to_canonical_route
method. This method retrieves the record from the controller's instance variables, then generates a canonical path using the polymorphic_path
method. If the current request path doesn't match the canonical path, the method issues a permanent redirect to the canonical path using redirect_to
.
This concern provides a way to ensure that URLs with old or outdated slugs are redirected to the latest version of the URL. This can be helpful for maintaining SEO and ensuring that users are always directed to the correct page.
I hope it is helpful :)