如何在模块中使用'before_action' [英] How to use 'before_action' in a module

查看:68
本文介绍了如何在模块中使用'before_action'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在模块中使用'before_action'。



不幸的是,我无法完成它。



我是googleing,但是我发现的一切都无法解决问题。



我的模块文件如下所示:

  module ShowController 
include SimpleController
#before_action:set_object,only:[:show]

def show
set_object
end
end

我想使用注释before_action行而不是show方法。



因此,我试图包含以下模块:

  include AbstractController :: Callbacks 
include ActiveSupport :: Callbacks
include ActiveSupport :: Concern
include ActiveSupport

另外,我试图require'active_support / all'或者core_ext。



我收到的error_message是:

<$ p对于SimpleController :: ShowController:Module
未定义的方法`class_attribute'

最后,什么都没有解决,我也找不到解决方案。 我认为这就是你想要做的:

  class SomeController< ActionController :: Base 
包含SimpleController
结束
$ b $模块SimpleController
扩展ActiveSupport ::关注

包含做
before_action: set_object,only:[:show]
end
end


I would like to use 'before_action' in a module.

Unfortunately, I couldn't get it work.

I was googleing, but everything I found couldn't solve the problem.

My module file looks like the following:

module ShowController
  include SimpleController
  #before_action :set_object, only: [:show]

  def show
   set_object
  end
end

I would like to use the outcommented before_action line instead of the show method.

Therefore, I was trying to include the following modules:

  include AbstractController::Callbacks
  include ActiveSupport::Callbacks
  include ActiveSupport::Concern
  include ActiveSupport

Additionally, I tried to "require 'active_support/all'" or the core_ext.

The error_message I receive is:

 undefined method `class_attribute' for SimpleController::ShowController:Module

Finally, nothing worked out and I didn't find a solution.

解决方案

I think this is what you're trying to do:

class SomeController < ActionController::Base
  include SimpleController
end 

module SimpleController
  extend ActiveSupport::Concern

  included do
    before_action :set_object, only: [:show]
  end
end

这篇关于如何在模块中使用'before_action'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆