从模块访问会话变量或控制器变量
使用:Rails 3.0.3
我的网站有这个功能(countcalculate.com),其中包含许多不同的计算。用户可以通过表单设置自己的小数变量(有效数字)。首先,我将此变量设置为配置变量,结果证明这是一个坏主意(某些用户所做的更改已传递给所有用户)。
因此,我决定将其设为会话变量,这是有意义的,因为用户应该选择自己的十进制值。
现在,在控制器中,我调用一个模块函数,该函数根据 id 依次调用 100 个不同的模块函数。
我的问题:
我想访问该模块中的这个会话变量,但事实证明这是不可能的。从控制器(通过模型)传递它有点像一场噩梦,因为它需要为每个函数传递。
那么:
- 如何从 lib-catalog 中的模块访问会话变量?
- 如何从模块访问控制器方法?如果是这样,我可以调用该方法并获取会话变量。
Using: Rails 3.0.3
I have this feature of my website (count calculate . com) which consists of plenty different calculations. The user can set his own variable for decimals (significant numbers) through a form. First I set this variable as a configatron variable which turned out to be a bad idea (a change that some user did was passed on to all users).
So, I decided to make it a session variable, which makes sense since a user should select his own decimal value.
Now, in the controller I call a module function that in turn calls 100 different module functions, depending on id.
My problem:
I want to access this session variable in that module which turns out to be impossible. Passing it already from the controller (through the model) is a bit of a nightmare since it needs to be passed for each and every function.
So:
- How do I access session variables from a module in the lib-catalog?
- How could I access a controller method from a module? If so I could call that method and fetch the session variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的意思是红宝石模块吗?就像在 mixin 中一样?如果您将其包含在控制器中,则它已经可以访问控制器有权访问的所有内容。只需调用
session
、request
、params
等即可正常工作。如果您确实需要从系统的任意部分获取会话,您可以通过以下方式获取请求:
然后您可以从请求中获取会话。 (请注意,请求实际上是一个伪装的单例......如果已经构建了一个请求,它将不会构建新的请求)。
Do you mean a ruby module? As in a mixin? If you're including it in your controller, it already has access to everything the controller has access to. Just call
session
,request
,params
etc and all will just work.If you really need to get the session from some arbitrary part of the system, you can obtain the request with:
Then you can get the session from the request. (Note that request is actually a singleton in disguise... it won't build a new request if one is already built).
这是一种方法,将您的计算模块混合到控制器中
包含计算方法的模块需要访问会话,因为您将其混合到控制器中,您可以使用 self
note self.user_value 调用 来调用 user_value 方法,该方法未在模块中定义,但您需要在您混合的类中定义它,
如果您的任何模型对象也定义了 user_value 方法,您可以将计算模块混合到那些需要的情况下,它们可能不会从会话中获取值
还有其他东西考虑一下,只需将用户定义的值存储在数据库而不是会话中,您就可以轻松地从模型或控制器访问该值
Here is one way to do it, mix your calculation module into the controller
The module that contains your calculation methods needs access to the session, since you are mixing it into the controller you can call the user_value method with self
note the self.user_value call, that method is not defined in the module but you will need to define it in the class you mixin to
if any of your model objects also define a user_value method you could then mixin the calculation module to those if needed, they would probably not be getting the value from session
Something else to consider, just store the user defined value in the database instead of the session, you will have easy access to the value from the model or controller
听起来您的模型包含模块代码。您真的不想直接从那里使用或访问会话。它破坏了 MVC 的目的,Rails 正在尽最大努力阻止您尝试类似的事情。
当你说“我调用一个模块函数,该函数又根据 id 调用 100 个不同的模块函数。”我想看看那段代码。
您最初的想法是正确的...该值需要传递给模型...听起来代码需要稍微干燥一下,这样就不那么令人头痛了。
不看代码很难给出建议。我陷入了类似的情况,并使用动态方法(覆盖 method_missing)来解决这个问题。
it sounds like your Model is including the module code. You really really do not want to use or access the session directly from there. It breaks the purpose for MVC, Rails is doing its best to prevent you from trying something like that.
when you say " I call a module function that in turn calls 100 different module functions, depending on id." I would like to see that chunk of code.
Your original idea is the correct one...the value needs to be passed over to the model...it just sounds like the code needs to be DRY'd up a little so that it isn't such a headache.
It is hard to give recommendations without seeing the code. I was stuck in a similar situation and used dynamic methods (overriding method_missing) to get around the problem.