首页 > 解决方案 > 在一类中访问 c​​urrent_user

问题描述

我正在使用 gem csv-importer,它为我提供了一个用于将 csv 数据导入我的 rails 数据库的类

但是,当我上传 csv 文件时,我需要知道当前用户,但 Rails 没有授予对类中 current_user 的访问权限

我想在调用时将自定义参数(例如 current_user 数据)从我的控制器传递到 import_csv 类,我该怎么做?

使用 import_csv 类调用

import = ImportUserCSV.new(file: file)
import.run!

我尝试了一个解决方案,其中我有 current_user 可用于所有类,这适用于 localhost 但在 Heroku 上中断

标签: ruby-on-railsclassparametersmodel

解决方案


尝试

import = ImportUserCSV.new(file: my_file) do
  after_build do |user|
     # here you have access to current_user 
  end
end

推荐阅读