Background: Generally, in situations where we want to manually specify the HTTP status code to be returned in the response, Rails gives us a nice set of pre-defined human-readable Ruby symbols to use, rather than explicitly using the numeric values for those codes.
We can do something like the following, for example:
render text: "hurray!", status: :ok
render text: "hurray!", status: 200
NEEDS_UPDATE_CODE = 242
render text: "whatever I want to render", status: NEEDS_UPDATE_CODE
I found the answer whilst writing the question. Since I cannot find any duplicates presently on SO, I shall post this answer in case anyone else ever has the same question.
The initial set of symbol representation-HTTP status code mappings is located in the Rack::Utils module, in an accessible hash called
SYMBOL_TO_STATUS_CODE. Additionally, the human-readable status code mappings is located in
In order to use the symbolic representation in my code, I simply added this in my initializer:
Rack::Utils::SYMBOL_TO_STATUS_CODE[:application_needs_update] = 242
Which allows me to use that symbol like so:
render text: "whatever I want to render", status: :application_needs_update
Unfortunately, the rails log will only show the status code, for example:
Completed 242 in 363ms (Views: 8.6ms | ActiveRecord: 12.0ms)
Which is not terribly helpful for someone who is not familiar with my custom code. In order to rectify that, I can add this in the same file:
Rack::Utils::HTTP_STATUS_CODES = "Application Needs Update"
As a result, when a request is completed using this code, my log will show this:
Completed 242 Application Needs Update in 363ms (Views: 8.6ms | ActiveRecord: 12.0ms)