Home > OS >  How to accurately define Controller name in Rails?
How to accurately define Controller name in Rails?

Time:11-16

Apologies for the basic question, but I'm trying to create an endpoint so I can a TranslationApi in my backend via my VueJs frontend via Fetch, so I need to make an endpoint I can insert. I'm attempting to create a route to make that happen, however when I run bin/rails routes | grep CcApiController I receive the following error:

ArgumentError: 'CcApiController' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use

I've read the documentation linked, but I'm not managing to fix this, can someone explain where I'm going wrong here? I'll link the files I've changed below:

cc_apis_controller.rb

module Panel
  class CcApisController < CcenterBaseController
    def index
      run Ccenter::Adapters::Zendesk::TranslationApi.call(2116449)
    end
  end
end

panel_routes.rb

def draw_api_routes
  resources: CcApiController
end

routes.rb

Rails.application.routes.draw do
resources :CcApiController, only: [:index]
end

API method I need to create Route for:

def make_request
    response = Faraday.post('https://api.deepl.com/v2/translate', auth_key: '', text: @final_ticket, target_lang: 'DE', source_lang: 'EN')
    if response.status == 200
      body = response.body
      message_element = body.split('"')[-2]
      return message_element
    else
      raise InvalidResponseError unless response.success?
    end
  end

CodePudding user response:

The answer to that is pretty simple. Route names are snake_case, and match the controller's file name just omit the _controller suffix. In your case it is

Rails.application.routes.draw do
  namespace :panel do
    resources :cc_apis, only: %i[index]
  end
end

For more info check this documentation and this article.

  • Related