【问题标题】:controller concerns in rails 4, sharing actionsRails 4 中的控制器关注点,共享操作
【发布时间】:2014-05-06 10:40:13
【问题描述】:

我正在尝试使用以下问题在控制器之间共享操作:

module Backend
  module Exportable
    extend ActiveSupport::Concern

    def show
      respond_to do |format|
        format.xls { set_excel_headers "#{controller_name.classify}_#{params[:id]}_#{Time.now.to_i.to_s}.xls" }
      end
    end
  end
end

你觉得这有什么问题吗?

我不应该通过关注点共享 Rails 的默认操作吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 controller activesupport-concern


    【解决方案1】:

    您必须在 concerns 目录中创建一个文件(名称与您的模块相同:backend.rb

    这是你的代码:

    module Backend
        def show
            respond_to do |format|
                format.xls { set_excel_headers "#{controller_name.classify}_#{params[:id]}_#{Time.now.to_i.to_s}.xls" }
            end
        end
    end
    

    所以在你的控制器中添加:

    extend Backend
    

    【讨论】:

    • 对不起,我的问题不是我如何做到这一点。此代码正在运行,后端是一个引擎。我要问的是定义这样的事情是否有意义。对 show、index 或 w/e 等内部默认操作的担忧!
    • 很抱歉误解了您的问题.. 但现在我们没事了。我认为如果“显示”动作在所有控制器中都相同,那么您使用的内容是有意义的。我的意思是你可以这样做来应用 DRY 原则
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多