如何将会话变量传递给 RoR 中的模型?

发布于 2024-09-04 15:36:27 字数 318 浏览 7 评论 0原文

我之前在应用程序中使用了全局变量来传递信息。但我遇到了一个问题,感谢这里的每个人建议我将这些数据存储在数据库的会话中。

我尝试了一下,但发现无法访问Model中的session变量。我搜索了一下,知道这是模型的正常行为,RoR 不会将会话变量传递给模型。

所以,我想在验证中使用该会话变量以及控制器......

  1. 如何传递值 会话变量进入模型?或者

  2. 还有其他方法适合我吗 用例?我需要一个变量存储 一个值,这是所有都需要的 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....

  1. how to pass the value of the
    session variable into Models? or

  2. is 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀梦 2024-09-11 15:36:27

如果我理解正确的话,会话变量会改变您验证模型的方式。我相信正确的解决方案如下:

class Blog < ActiveRecord::Base
  attr_accessor :validate_title

  validate_presence_of :title, :if => :validate_title
end

class BlogsController < ApplicationController
  def new
    @blog = Blog.new
    @blog.validate_title = session[:validate_title]
  end
end

代码尚未经过测试,但这就是想法。 if 参数可以是方法的名称,您可以在其中执行任何您想要的操作。如果需要,您可以使用各种验证模式。例如:

class Blog < ActiveRecord::Base
  attr_accessor :validation_mode

  validate_presence_of :title, :if => :validate_title

  def validate_title
    validation_mode == "full" or validation_mode == "only_title"
  end
end

class BlogsController < ApplicationController
  def new
    @blog = Blog.new
    @blog.validate_mode = session[:validate_mode]
  end
end

有关详细信息,请阅读验证指南

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:

class Blog < ActiveRecord::Base
  attr_accessor :validate_title

  validate_presence_of :title, :if => :validate_title
end

class BlogsController < ApplicationController
  def new
    @blog = Blog.new
    @blog.validate_title = session[:validate_title]
  end
end

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:

class Blog < ActiveRecord::Base
  attr_accessor :validation_mode

  validate_presence_of :title, :if => :validate_title

  def validate_title
    validation_mode == "full" or validation_mode == "only_title"
  end
end

class BlogsController < ApplicationController
  def new
    @blog = Blog.new
    @blog.validate_mode = session[:validate_mode]
  end
end

For more information, read the guide on validation.

醉酒的小男人 2024-09-11 15:36:27

您是否需要会话变量作为模型的一部分,或者只是作为确定要执行的内容的标志?

如果是前者,您不需要知道参数的来源,只需将变量作为参数传递给方法中的某些调用即可。例如:

@user = User.new(params[:user].merge(:some_attribute => session[:some_key])

并像往常一样在模型中添加验证。

如果您需要为了控制某些执行流程,正如您提到的,对于不同的用户应该有所不同,您可能需要在控制器中使用实例变量,例如

class SomeController
  before_filter :set_some_session_variable

  def set_some_session_variable
    @some_variable = session[:some_key]
  end
end

您可以直接在您的控制器中使用 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

class SomeController
  before_filter :set_some_session_variable

  def set_some_session_variable
    @some_variable = session[:some_key]
  end
end

You could use session[:some_key] directly in your view, but is better to set it in an instance variable instead.

三岁铭 2024-09-11 15:36:27

最好的方法是创建一个带有参数的方法并传递会话作为参数

例如

class User < ActiveRecord::Base

  def self.some_method(some_arg)
     User.find(some_arg)
  end

end

来自控制器的示例

User.some_method(session[:user_id])

Best way of doing this is to Create a method with an argument and pass session as an argument.

Fro example

class User < ActiveRecord::Base

  def self.some_method(some_arg)
     User.find(some_arg)
  end

end

from controller

User.some_method(session[:user_id])
蹲在坟头点根烟 2024-09-11 15:36:27

模型是数据库的接口。它们可以在没有会话的情况下使用(例如从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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文