Rails 控制器中的“env"和“request.env"有什么区别? [英] What is the difference between 'env' and 'request.env' in Rails Controller?

查看:27
本文介绍了Rails 控制器中的“env"和“request.env"有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 rspec 为我的应用程序编写测试.为了授权,我在标题中发送 token:

I'm using rspec to write test for my application. In order to authorization I'm sending token in header:

request.headers['token'] = '000000099'
get :index

在控制器端,我可以通过 request.env["HTTP_TOKEN"] 读取这个值,但是 env["HTTP_TOKEN"] 是空的.

In controller side I can read this value by request.env["HTTP_TOKEN"] but env["HTTP_TOKEN"] is empty.

它们之间有什么区别,我该如何设置 env 而不是 request.env?

What is the difference between them and how can I set env instead of request.env?

推荐答案

request.env 是一个 ruby​​ 数组,其中包含有关访问用户和服务器环境的信息.request.env 是 Rails 应用中用于提取重要信息的标准对象,例如 path_inforequest_uri 等.

request.env is a ruby array that contains information about a visiting user’s and server environments. request.env is the standard object that's being used in Rails app to extract important information such as path_info, request_uri etc.

env 对于您的测试来说是空的,因为 rspec-rails 绕过了 ActionController::Metal 调度方法.

env is empty for your test because rspec-rails bypasses the ActionController::Metal dispatch method.

这篇关于Rails 控制器中的“env"和“request.env"有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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