如何在 Rails 模型中获取 request.uri? [英] How do i get request.uri in model in Rails?

查看:56
本文介绍了如何在 Rails 模型中获取 request.uri?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$request = request

当我在控制器中写这个时,它会起作用.但是如果我在模型或应用程序控制器中需要这个变量,我该怎么做?

When I write this in controller, it will work. But if i need this variable in Model or Application controller, How can i ?

推荐答案

模型存在于 Web 请求的上下文之外.您可以在 irb 中实例化它们,也可以在延迟作业或脚本等中实例化它们.如果模型依赖于请求对象,那么这些都将无法实现.

Models exist outside the context of a web request. You can instantiate them in irb, you can instantiate them in a delayed job, or a script, etc. If the model depended on the request object, none of these things would be possible.

正如 tsdbrown 所说,您必须以某种方式从使用模型的上下文中传递该信息.

As tsdbrown says, you have to somehow pass in that information from the context that uses the model.

这篇关于如何在 Rails 模型中获取 request.uri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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