ruby-on-rails - Rails 5:将 HTTParty 查询限制为 current_user
问题描述
我目前正在实现 Typeform 的 API。我能够获取响应,但我需要向用户显示响应分配。
当一个 typeform 被提交时,一个hidden_field
被传递给 Typeform 这是current_user.hash_id
.
到目前为止一切都很好。我还可以使用parameter
调用的 API 设置限制query
。
所以我的请求目前看起来像:
def get_data
self.class.get("/forms/XXXXX/responses?page_size=25&query=", headers: { "Authorization" => @auth})
end
我需要添加current_user.hash_id
到.&query=
current_user
因此,我应该将逻辑传递给 mycontroller
以使其current_user
易于访问,还是有另一种方法可以current_user
安全地调用 my model
?
解决方案
导轨 5.2
所以我做了以下事情:
我创建了一个module
访问lib
权限:user
module Current
thread_mattr_accessor :user
end
后来我把这个添加到我的ApplicationController
around_action :set_current_user
def set_current_user
Current.user = current_user
yield
ensure
Current.user = nil
end
您还需要添加config.autoload_paths
到您的àpplication.rb
config.autoload_paths += %W(#{config.root}/lib)
之后,您需要重新启动服务器。
最后,您可以访问Current.user
您的模型,例如:
def get_data
self.class.get("/forms/XXXXX/responses?page_size=25&query=#{Current.user.hash_id}", headers: { "Authorization" => @auth})
end
推荐阅读
- android - 启用“Google Play 应用签名”后无法更新游戏
- vb.net - Google Drive API:用户使用 API 密钥对此文件没有足够的权限
- python - 检测不在创建的颜色图中的颜色
- python - 如何在 NLP 中同时进行数据增强和交叉验证?
- python - 在python中增加日期
- facebook - 使用令牌从网页链接到 facebook messenger
- javascript - 如何添加JS“addEventListener”?
- node.js - 从 NodeJs 获取数据到 Angular
- php - 来自主域和子域的 PHP 会话
- javascript - 为什么我最新的 css 文件没有在我的浏览器中更新?