【问题标题】:How add Helpers in API-only Rails Application如何在仅 API 的 Rails 应用程序中添加助手
【发布时间】:2017-07-04 20:22:46
【问题描述】:

我创建了一个仅 API 的 Rails 应用程序,但我需要一个管理区域来管理数据。所以我创建了这个控制器:

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 我无法访问 Helper,即使需要该模块。找帮手:

module AdminHelper
  def teste
    'ok'
  end
end

并且产生的错误:

ActionController::RoutingError (uninitialized constant AdminController::AdminHelper):

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5


    【解决方案1】:

    因此,我能够在运行 rails new my_api_test_app --api 的新应用程序中完成这项工作,然后包含以下文件。我认为您不需要控制器中的 require 语句。你可以像你所做的那样包含助手。我已经包含了我用于每个文件的文件结构位置(值得注意的是,我将帮助程序放在 app/helpers/admin_helper.rb 中,这可能是您正确加载文件所需要的。

    #app/controllers/admin_controller.rb
    class AdminController < Rails::ApplicationController
      include AdminHelper
      def index
        test
        render file: Rails.root.join('app', 'views', 'admin', 'index.html')
      end
    end
    
    
    #app/helpers/admin_helper.rb
    module AdminHelper
      def test
        puts "tests are really fun"
      end
    end
    
    #config/routes
    Rails.application.routes.draw do
      root 'admin#index'
    end
    
    #index.html.erb
    Hello World!
    

    在 rails 日志中,我得到了这个:

    app/controllers/admin_controller.rb:5:in `index'
    Started GET "/" for 127.0.0.1 at 2017-02-15 15:26:32 -0800
    Processing by AdminController#index as HTML
    tests are really fun
      Rendering admin/index.html.erb within layouts/application
      Rendered admin/index.html.erb within layouts/application (0.3ms)
    Completed 200 OK in 8ms (Views: 8.0ms | ActiveRecord: 0.0ms)
    

    请注意,tests are really fun 打印在日志中。

    【讨论】:

      【解决方案2】:

      如果您使用ActionController::API(并且在实现 API 时应该这样做),您可以通过包含专用的 mixin 来使用应用程序助手:

      class Api::V1::ApiController < ActionController::API
        include ActionController::Helpers
        helper MyHelper
      end
      

      【讨论】:

        【解决方案3】:

        完整示例:

        在:app/controllers/admin_controller.rb

        class AdminController < ActionController::API
          include ActionController::Helpers
          helper AdminHelper
          def index
            test = ApplicationController.helpers.test('test')
            render json: test
          end
        end
        

        在:app/helpers/admin_helper.rb

        module AdminHelper
          def test(args)
            return args
          end
        end
        

        您可以使用rails generate rspec:helper 生成测试

        RSpec.describe AdminHelper, type: :helper do
          it "return args" do
            expect(helper.args('yo'))
                .to eq('yo')
          end
        end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-20
          • 1970-01-01
          • 2012-01-30
          • 1970-01-01
          • 1970-01-01
          • 2011-05-28
          相关资源
          最近更新 更多