0

Some bots or other external requests cause an ActionController::UnknownFormat error when redirecting

Is there a catch-all method to add somewhere in application_controller to handle those errors ?

A config spec to add a default method in request (if none specified) ?

Below an example of trace :

An ActionController::UnknownFormat occurred in main#index:

  ActionController::UnknownFormat
  app/controllers/main_controller.rb:17:in `index'


-------------------------------
Request:
-------------------------------

  * URL        : http://ownedurl.com/
  * HTTP Method: GET
  * IP address : 82.xxx.xxx.xxx
  * Parameters : {"controller"=>"main", "action"=>"index"}
  * Timestamp  : 2015-06-07 20:54:32 +0200
  * Server : servername
  * Rails root : /var/www/ownedurl.com/releases/208
  * Process: 29726

A get request would obviously work; yet it makes it occurs.

Experiences on this one appreciated thx a lot

Ben
  • 5,030
  • 6
  • 53
  • 94

2 Answers2

1

This will handle a request without format :

rescue_from ActionController::UnknownFormat, with: :raise_not_found
def raise_not_found
    render(text: 'Not Found', status: 404)
end
Ben
  • 5,030
  • 6
  • 53
  • 94
0

You can supply the :format to your routes itself to tell it what are the formats are valid and which are not.

Read some discussions on this and this answer.

Community
  • 1
  • 1
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • good point. main#index is actually the root of the app. what's the syntax to specify the format in it ? – Ben Jun 07 '15 at 19:11
  • @Ben did you check ?/ – Arup Rakshit Jun 07 '15 at 19:37
  • I'll check tomorrow. Added `root :to => 'main#index', :defaults => { :format => 'html' }`. Thing is, I don't know how to address a request without format with curl or other similar tool – Ben Jun 07 '15 at 22:22