简体   繁体   English

如何在Ruby on Rails控制台中使用app.get()设置会话变量?

[英]How to set session variables with app.get() in Ruby on Rails console?

How can I manually set session while calling app.get() in script/console ? 如何在script/console调用app.get()时手动设置会话?

For example I want to access an authorised page, but it always redirects me to sign-in page. 例如,我想访问一个授权页面,但是它总是将我重定向到登录页面。 How can I set the session before calling app.get() so that I can make an authorised call to that page? 如何在调用app.get()之前设置会话,以便可以对该页面进行授权调用?

如果将临时文件放入其中,则该会话是可读的( http://tektastic.com/2006/12/ruby-on-rails-how-to-load-session.html ),但除非使用调试器,否则否会话散列的存储方式不能通过控制台进行修改。

depends on what you are using for session authentication. 取决于您用于会话身份验证的内容。 if you do app.class , you will see that app is an ActionController::Integration::Session . 如果执行app.class ,您将看到该应用程序是ActionController::Integration::Session What you need to do is authenticate yourself, then hitting your restricted pages should work 您需要做的是对自己进行身份验证,然后点击受限制的页面即可

Here is an ugly workaround that I did. 这是我做的一个丑陋的解决方法。

#Type in script/console
include ActionController::TestProcess
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@controller = SomeController.new
get 'action', {}, {:signed => 42} #action, params, session
#now @response has the updated response object.

Before doing another get()/post() etc make sure you reset @controller by calling @controller = SomeController.new , or else @response doesn't seem to get updated on subsequent requests. 在执行另一个get()/post() etc确保通过调用@controller = SomeController.new重置@controller ,否则@response似乎不会在后续请求中得到更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM