You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And you are using CanCanCan gem to implement authorization in your application. So, in your SpeakersController you are loading and authorizing the resources like this:
classSpeakersController < ApplicationControllerload_and_authorize_resource# GET /speakersdefindex;end# GET /speakers/1defshow;endend
And you are using the same method to load and authorize resources in other controllers.
How you can utilize the history plugin from FiendlyId gem to redirect your old slugs like /speakers/falde-ali to new slugs like /speakers/ali-fadel when you change the speaker name, without implementing a specific method inside each controller to handle it?
Answer:
Basically, you can implement a concern inside app/controller/concerns like this:
Then include it into your controllers below the load and authorization call, like this:
classSpeakersController < ApplicationControllerload_and_authorize_resourceincludeSlugRedirector# GET /speakersdefindex;end# GET /speakers/1defshow;endend
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 :)
What do you think about the approach and about adding this concern as a plugin inside FriendlyId ?.?
The text was updated successfully, but these errors were encountered:
Question:
Let's say you have a model
Speaker
that utilizes FiendlyId gem to create unique slugs like this:And you are using CanCanCan gem to implement authorization in your application. So, in your
SpeakersController
you are loading and authorizing the resources like this:And you are using the same method to load and authorize resources in other controllers.
How you can utilize the
history
plugin from FiendlyId gem to redirect your old slugs like/speakers/falde-ali
to new slugs like/speakers/ali-fadel
when you change the speaker name, without implementing a specific method inside each controller to handle it?Answer:
Basically, you can implement a concern inside
app/controller/concerns
like this:Then include it into your controllers below the load and authorization call, like this:
This concern implements a redirect to the latest slug using the FriendlyId gem.
The concern includes a
before_action
that calls theredirect_to_canonical_route
method. This method retrieves the record from the controller's instance variables, then generates a canonical path using thepolymorphic_path
method. If the current request path doesn't match the canonical path, the method issues a permanent redirect to the canonical path usingredirect_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 :)
What do you think about the approach and about adding this concern as a plugin inside FriendlyId ?.?
The text was updated successfully, but these errors were encountered: