如何将会话变量传递给 RoR 中的模型?
我之前在应用程序中使用了全局变量来传递信息。但我遇到了一个问题,感谢这里的每个人建议我将这些数据存储在数据库的会话中。
我尝试了一下,但发现无法访问Model中的session变量。我搜索了一下,知道这是模型的正常行为,RoR 不会将会话变量传递给模型。
所以,我想在验证中使用该会话变量以及控制器......
如何传递值 会话变量进入模型?或者
还有其他方法适合我吗 用例?我需要一个变量存储 一个值,这是所有都需要的 MVC,并且应该是独立的 不同并发用户之间。
谢谢大家。 :)
I used a global variable in my app for passing information before. But I got a problem and thanks everyone here suggested me to store those data in session with database.
I tried, but I found that I can't access the session variable in Model. I googled and knew this is the Model normal behavior, RoR won't pass the session variable to Model.
So, I would like to use that session variable in validation and also the controller....
how to pass the value of the
session variable into Models? oris there any other method for my
use case? I need a variable storing
a value, which is required in all
MVCs, and should be independent
between different concurrent users.
Thanks everyone. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解正确的话,会话变量会改变您验证模型的方式。我相信正确的解决方案如下:
代码尚未经过测试,但这就是想法。 if 参数可以是方法的名称,您可以在其中执行任何您想要的操作。如果需要,您可以使用各种验证模式。例如:
有关详细信息,请阅读验证指南。
If I understand you correctly, a session variable changes the way you validate the model. I believe the correct solution for this is the following:
The code has not been testet, but that's the idea. The if argument can be the name of a method and you can do whatever you want in there. You can have various validation modes if you want. For example:
For more information, read the guide on validation.
您是否需要会话变量作为模型的一部分,或者只是作为确定要执行的内容的标志?
如果是前者,您不需要知道参数的来源,只需将变量作为参数传递给方法中的某些调用即可。例如:
@user = User.new(params[:user].merge(:some_attribute => session[:some_key])
并像往常一样在模型中添加验证。
如果您需要为了控制某些执行流程,正如您提到的,对于不同的用户应该有所不同,您可能需要在控制器中使用实例变量,例如
您可以直接在您的控制器中使用
session[:some_key]
。视图,但最好将其设置在实例变量中。Do you need the session variable as part of your model, or just as a flag to determine what to execute?
If the former, you don't need to know where did the parameters originate, just pass the variable as an argument for some call in your method. For example:
@user = User.new(params[:user].merge(:some_attribute => session[:some_key])
and just add the validation as usual in the model.
If you need that to control some flow of the execution, as you mention that should be different for different users, you may need an instance variable in your controller. Something like
You could use
session[:some_key]
directly in your view, but is better to set it in an instance variable instead.最好的方法是创建一个带有参数的方法并传递
会话作为参数
。例如
来自控制器的示例
Best way of doing this is to Create a method with an argument and pass
session as an argument
.Fro example
from controller
模型是数据库的接口。它们可以在没有会话的情况下使用(例如从IRB)。允许模型与会话对话将违反 MVC。您能否通过更多有关您想要做什么的信息来扩展您的问题?很可能有更好的方法来做到这一点。
Models are an interface to the database. They can be used without a session (e.g. from IRB). It would be a violation of MVC to allow the models to talk to the session. Can you expand your question with a bit more info about what you're trying to do? There is most probably a better way to do it.