【问题标题】:Create bare rails controller class创建裸rails控制器类
【发布时间】:2014-04-09 11:55:07
【问题描述】:

我正在尝试基于ActionController::Base 创建干净的控制器。这就是我尝试的方法:

class MetalController
   ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
     include left
   end
end

来自 Rails 文档:

返回包含在 ActionController::Base 除了作为参数传递的那些:

这可以更好地控制您要排除的内容并使其 更容易创建一个裸控制器类,而不是列出模块 手动需要。

我的另一个控制器继承自MetalController

class API::BaseController < MetalController
  #.... my awesome api code
end

所以这不起作用然后我启动rails server

block in &lt;module:AssetPaths&gt;': undefined methodconfig_accessor' 为 MetalController:Class (NoMethodError)

Rails 4.1.0、Ruby 2.1.0

更新:

如果我include ActiveSupport::Configurable

抛出错误:

_implied_layout_name': undefined local variable or method controller_path' 用于 MetalController:Class (NameError)

【问题讨论】:

  • 您是否尝试将 include ActiveSupport::Configurable 添加到您的 MetalController(在每个循环之前)?
  • @BroiSatse 不起作用(。查看更新问题

标签: ruby-on-rails ruby api actioncontroller


【解决方案1】:

你需要从ActionController::Metal继承:

class MetalController < ActionController::Metal
  ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
    include left
  end
end

【讨论】:

  • 我不这么认为,因为ActionController::Base::MODULES 中的订单看起来与ActionController::Base.without_modules(:ParamsWrapper, :Streaming) 相同。我知道.ancestors,谢谢你的解决方案,但我有同样的错误。
  • 嗯。它正在为我运行,没有任何问题。
  • 抱歉,在您解决方案后,MetalController:Class` 出现另一个错误_implied_layout_name': undefined local variable or method controller_path',您知道吗?
  • 你也需要继承 AbstractController::Base 。我会更新答案。
  • 我找到了解决方案,如果我使用 AbstractController::Base 而不是我的控制器 raise undefined method `action',我需要从 ActionController::Metal 继承
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多