Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add Helpers in API-only Rails Application

I created a API-Only Rails Application but I need a administrative area to manage data. So I created this controller:

require 'rails/application_controller'
require_relative '../helpers/admin_helper'
class AdminController < Rails::ApplicationController
  include AdminHelper
  def index
    @q = Promotion.search(params[:q])
    @promotions = @q.result(distinct: true).page(params[:page]).per(30)
    render file: Rails.root.join('app', 'views', 'admin', 'index.html')
  end
end

Bot I can't access Helper, even requiring the module. Look a Helper:

module AdminHelper
  def teste
    'ok'
  end
end

And the error generated:

ActionController::RoutingError (uninitialized constant AdminController::AdminHelper):
like image 424
Luiz Carvalho Avatar asked Nov 05 '25 20:11

Luiz Carvalho


1 Answers

If you are using ActionController::API (and you should when implementing APIs), you can make use of application helpers by including the dedicated mixin :

class Api::V1::ApiController < ActionController::API
  include ActionController::Helpers
  helper MyHelper
end
like image 92
Ghis Avatar answered Nov 07 '25 08:11

Ghis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!