开发者

rails - redirect when user manually inputs wrong url

开发者 https://www.devze.com 2023-01-05 00:48 出处:网络
How can I redirect a misspelled url开发者_如何学Python to some default url in case a user mistypes a url?You can add a default root, catch all routes.

How can I redirect a misspelled url开发者_如何学Python to some default url in case a user mistypes a url?


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消