如何在环境配置中使用辅助方法? [英] How to use helper method in environment configuration?

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

问题描述

我在 application_controller.rb 中有以下辅助方法:

I have the following helper method in application_controller.rb:

class ApplicationController < ActionController::Base
  def current_tenant
    @current_tenant ||= Tenant.find_by(domain: Apartment::Tenant.current)
  end
  helper_method :current_tenant
end

租户表还存储了一些我想在环境配置中使用的信息,如下所示(这是development.rb):

The tenant table also stores some information that I want to use in the environment configuration like so (this is development.rb):

Rails.application.configure do

  config.paperclip_defaults = {
    :s3_credentials => {
      :bucket => current_tenant.amazon_s3_bucket,
      :access_key_id => current_tenant.amazon_s3_id,
      :secret_access_key => current_tenant.amazon_s3_secret
    }
  }

end

这给了我这个错误:

NameError: undefined local variable or method 'current_tenant'

是否可以定义一种可以在视图环境配置中使用的方法?

Is it possible to define a method which can be used in views AND environment configuration?

推荐答案

你不能,在这种情况下它根本没有意义.回形针默认值设计为独立于请求的全局配置默认值.

You can't, and in this case it doesn't make sense at all. The paperclip defaults are designed to be request-independent, global configuration defaults.

在您的情况下,您必须将默认值保留为空,并确保根据用户将适当的配置传递给控制器​​实例.文档中对此进行了描述.

In your case, you must leave the defaults empty and make sure to pass in your controller the proper configurations to the paperclip instance according to the user. This is described in the documentation.

这篇关于如何在环境配置中使用辅助方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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