ruby-on-rails - 在一类中访问 current_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 上中断
解决方案
尝试
import = ImportUserCSV.new(file: my_file) do
after_build do |user|
# here you have access to current_user
end
end
推荐阅读
- r - 矩阵中特定行的行和
- wpf - 样式中的 WPF 命令绑定 ItemsControl
- arangodb - Arango 查询,带有边缘计数的集合
- ionic-framework - Ionic 3 图像映射问题
- python - Django 管理员。当对象具有布尔字段== True时,如何为列表视图中的每一行添加背景颜色?
- c - 具有 DSNAME 长度的 IBM Window Services (DWS) CSRIDAC 无效
- vb.net - 在文本框中显示光标指向的行并在 VB.NET 中隐藏其他行
- mysql - 如何根据其名称将一行除以三个计数之一?
- string - 在golang中使用==符号并使用循环比较字符串a是否等于字符串b,哪个性能更好?
- angularjs - 文件上传与其他表单数据获取文件未定义的angularjs