У меня есть помощник, контроллер и шаблон, как:Вызов помощника из шаблона RABL
Helper:
# app/helpers/application_helper.rb
module ApplicationHelper
def current_user
@current_user ||= User.find_by(access_token: access_token)
end
private
def access_token
pattern = /^Bearer/
header = request.headers["Authorization"]
header.gsub(pattern, "") if header && header.match(pattern)
end
end
Контроллер:
# app/controllers/api/v1/companies_controller.rb
class Api::V1::CompaniesController < Api::V1::BaseController
before_action :set_company, only: [:show]
def show
render @company
end
private
def set_company
@company ||= Company.find(params[:id])
end
end
# app/controllers/api/v1/base_controller.rb
class Api::V1::BaseController < ApplicationController
respond_to :json
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include ApplicationHelper
protect_from_forgery with: :null_session
end
RABL-Rails шаблон:
object :@company
attributes :id, :name, :description, :website
# --- How can I call a helper method here?
# if (@company.owner?(current_user) or current_user.kind_of?(Admin))
# attributes :contact
# end
attributes :created_at, :updated_at
Когда я вызываю вспомогательный метод из шаблона RABL, это вызовет ошибку:
undefined local variable or method `current_user' for #<RablRails::Compiler:0x00000002494c68>
Как я могу позвонить вспомогательный метод из шаблона RABL?
Примечание: Я использовал gem rabl-rails '~> 0.4.1'
.
На самом деле, 'Api :: V1 :: BaseController' расширяет' ApplicationController', который я положил 'включить ApplicationHelper' там. –