Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails - redirect when user manually inputs wrong url

How can I redirect a misspelled url to some default url in case a user mistypes a url?

like image 240
thenengah Avatar asked Jun 11 '26 23:06

thenengah


2 Answers

You can add a default root, catch all routes.

By example an extract from Typo source code :

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

In your controller you have a params[:from] which an Array of all params of your URL

like image 200
shingara Avatar answered Jun 13 '26 13:06

shingara


You can rescue ActionController::RoutingError from application_controller, like CanCan suggests 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
like image 37
Oinak Avatar answered Jun 13 '26 14:06

Oinak