Rails - redirection when the user manually enters the wrong URL

How can I redirect an error URL to some default URL if the user mistakenly uses the URL?

+3
source share
2 answers

You can save ActionController :: RoutingError from application_controller, for example CanCan offers for unauthorized access:

class ApplicationController < ActionController::Base

  (...)
  # Reditect to a default route when user inputs a wrong one
  rescue_from ActionController::RoutingError do |exception|
    flash[:error] = "There is no such route"
    redirect_to root_url
  end

end
+1
source

You can add default root, catch all routes.

Example example from Typo source code:

map.connect '*from', :controller => 'articles', :action => 'redirect'

In your controller, you have params [: from], which contains an array of all the parameters of your URL

+5
source

Source: https://habr.com/ru/post/1752403/


All Articles