如何在我的自定义lib文件中包含会话?

[英]How to include session in my custom lib file?


I know I will get answers that I shouldn't do this, but due to specific way to solve problem I am facing, I will have to use session in my /lib/example.rb file. (or at least I think I will have to use it)

我知道我会得到答案,我不应该这样做,但由于解决我面临的问题的具体方法,我将不得不在我的/lib/example.rb文件中使用session。 (或者至少我认为我将不得不使用它)

I am calling an action, which will first run (seudo code):

我正在调用一个动作,它将首先运行(seudo代码):

module ApplicationHelper

  def funcion(value)
    MyClass.use_this(value)
  end
end

And then I will use it in my lib/example.rb

然后我将在我的lib / example.rb中使用它

module MyClass
  # include SessionsHelper # this is not working

  def self.use_this(value)
    # I want to be able to use session here. What I need to do that in order to make it work.
    session[:my_value] = value
  end
end

What should I do in order to use session inside MyClass (I can pass variable to MyClass.use_this(value,session), but I wouldn't want to do that

我应该怎么做才能在MyClass中使用session(我可以将变量传递给MyClass.use_this(value,session),但我不想这样做

Edit:

What I want to achieve with this session thing is that I would like to preserve a value during multiple requests. I am making a call to the web application multiple times, and I want to preserve some value on the next call. I am calling the app via API and I shouldn't use database to save values. So I have left with sessions, or text files, or even maybe cookies to make this happen - to preserve the same value on multiple calls.

我希望通过此会话实现的目标是我希望在多个请求期间保留一个值。我多次调用Web应用程序,我想在下次调用时保留一些值。我通过API调用应用程序,我不应该使用数据库来保存值。所以我留下了会话,文本文件,甚至可能是cookie来实现这一点 - 在多个呼叫中保留相同的值。

2 个解决方案

#1


0  

Why not include the module in your controller, and then call the use_this function directly from there?

为什么不在控制器中包含模块,然后直接从那里调用use_this函数?

module MyClass #should probably rename this anyway
  def use_this(value)
    session[:my_value] = value
  end
end

class SomeController < ApplicationController
  include MyClass

  def some_action
    ...
    use_this(the_value)
    ...
  end
end

#2


-1  

In order to use session inside MyClass may be you could use instance variable @session:

为了在MyClass中使用session,你可以使用实例变量@session:

module MyClass
  extend SessionsHelper

  def self.use_this(value)
    @session[:my_value] = value
  end
end

module SessionsHelper
  def some_method
    @session = ...
  end
end

self.include(module) method makes the instance methods (and instance variables) of the included module into instance methods of the including module.

self.include(module)方法使包含模块的实例方法(和实例变量)成为包含模块的实例方法。

Edit: include SessionsHelper changed to extend SessionsHelper

编辑:包括SessionsHelper更改为扩展SessionsHelper

self.extend(module) -- methods of receiver become class methods of that class and instance variables will work between this methods.

self.extend(module) - 接收器的方法成为该类的类方法,实例变量将在这些方法之间起作用。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/03/19/d7df7a6c961bdbcfe754618536d56bbb.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告