开发者

ENV array/ user session

I have been provided with some sample code which stores data associated with teh current user in the ENV array:

ENV['token'] = request.env['omniauth.auth']['credentials']['token']

I'm new to rails, and as I understand it this is the Environment variables array that is passed to Ruby/Rails.

I assume this isn't an appropriat开发者_Python百科e place to put the info? Rather it should be in the users session?


I agree. If ENV is to be modified at all, it should be at the very beginning of the script's execution. Since this is data that changes for each request, it shouldn't be global. Not only is it weird, it could cause problems if your app ends up in a stack where multiple requests run at a time and this value changes before a request has finished using it.

Since we're using request, I bet we're in the context of a controller. I'd therefore recommend setting this to an instance variable @token that is accessible anywhere in your controller and views, but is tied to this request. That gives it a large scope, but no larger than you need.

This, of course, assumes that we're talking about controller code. If so, there you go, but I'd still like some more context to make sure we're not talking about, say, Rack middleware or something more complicated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜