moorara moorara - 1 month ago 9
Ruby Question

How to add a custom route, controller, and action in Ruby on Rails?

I have a Ruby on Rails and ActiveAdmin application. I didn't basically change any default configuration except adding and registering a few models.

I want to enable my application with a route like

GET /heartbeat
and respond with a simple string to client/user. I'm wondering how could I do the following steps:


  1. Add a custom route to my
    routes.rb
    file.

  2. Add a custom controller under
    app/controllers
    path.

  3. Implement a custom action and respond to user directly without any view.


Answer

routes.rb:

get 'heartbeat' => "custom_controller#heartbeat"

custom_controller.rb:

class CustomController < ApplicationController
  def heartbeat
    render inline: "Some string to the client/user"
  end
end