ruby on rails - RoR: how to catch any route that didn't match route config and redirect to homepage -


for example in routes have such code:

get "profile/:id" => 'profiles#show', as: :profile  root 'articles#index' 

but how catch such kind of url , redirect home:

i need catch non existing route. why need this? via seo rules. becouse example if enter url.com/?porn or url.com/profile?1&azaza - on page entered ugly url. need redirect home, "hacker" didn't not post illegal link's on resources...

is real do?

there 2 different cases need take care of

  • a wrong path
  • a wrong resource

for first one, can add global match rule @ end of routes.

match '*foo', to: 'articles#index' 

then should handle missing resources in controllers.

eg

def show   begin     @article = article.find(params[:id])   rescue     redirect_to root_url   end end 

of course if have multiple controllers , need repeat in many places should use more general approach. perhaps rescuing activerecord::recordnotfound on applicationcontroller


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -