簡體   English   中英

在Rails控制器中包括關注模塊

[英]Include a concern module in a rails controller

這是我關心的文件:controllerconcerns.rb

require 'active_support/concern'

module Query_scopes
  extend ActiveSupport::Concern
  has_scope :title
end

這是我要包含在其中的控制器:api_controller.rb

class ApiController < ApplicationController
  require 'concerns/controllerconcerns'
  include Query_scopes
  etc etc etc

這是我得到的錯誤:

undefined method `has_scope' for Query_scopes:Module

我已經安裝了has_scope gem,如果我在要應用到的每個控制器中都說'has_scope: scopename' ,它會很好地工作,那么如何將幾行“ has_scope”代碼應用於所有控制器?

您應該遵循使用關注點的命名約定,並在included do塊中包括您想要的內容:)

例如。

module QueryScopes
  extend ActiveSupport::Concern

   included do
     has_scope :title
   end
end

接着:

class ApiController < ApplicationController
  include QueryScopes
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM