【问题标题】:Rails application variables in carrierwave store_dircarrierwave store_dir 中的 Rails 应用程序变量
【发布时间】:2013-08-12 02:25:48
【问题描述】:

我希望使用 application_controller 中设置的应用程序变量

application_controller.rb

def current_company
 @current_company ||= Company.find(cookies[:default_company]) if cookies[:default_company]
end

我现在如何在上传器中使用 current_company.id (company_logo_uploader.rb)

def store_dir
 "uploads/#{current_company.id}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

我得到一个未定义的局部变量或方法 `current_company' 用于 :CompanyLogoUploader

我们将不胜感激。

【问题讨论】:

    标签: ruby-on-rails store carrierwave dir


    【解决方案1】:

    您必须调用在您的类上定义的实例方法,并在其前面加上 model. 定义

    试试这个

    "uploads/#{model.current_company.id}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"

    注意

    确保设置了@current_company,否则它将返回nil

    【讨论】:

    • 在他的示例中,current_company 是在 application_controller 中定义的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多