【发布时间】:2016-12-02 16:36:26
【问题描述】:
我在会话哈希中存储了一个计数器,每次我在Store 控制器中调用index 操作时,该计数器都会递增。
我在Store 视图中打印出来,如代码和屏幕截图所示。
我知道我可以通过删除 cookie 来“重置计数器”,如屏幕截图所示。而且我知道我在服务器上打印会话哈希。但是如何在 chrome 开发工具中查看会话哈希?
store_controller.rb
class StoreController < ApplicationController
def index
@products = Product.order(:title)
session[:counter] ||= 0
session[:counter] += 1
end
end
存储/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Your Pragmatic Catalog</h1>
<p><%= session[:counter] %></p>
<% p session %>
<% cache @products do %>
<% @products.each do |product| %>
<% cache product do %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
<%= button_to 'Add to Cart', line_items_path(product_id: product) %>
</div>
</div>
<% end %>
<% end %>
<% end %>
截图
【问题讨论】:
-
您使用什么会话存储策略?在任何情况下,不要期望能够从浏览器访问会话内容,内容并不总是在浏览器中,如果存在,它们将被加密以防止人们查看或更改东西。
-
不确定,我使用的是默认的 rails 5 策略。我读了a blog post,发现了这个rails guide,这似乎是对自己进行更多教育的好读物:)我认为会话存储在浏览器中,看起来它只是存储的cookie,所以它更复杂比我原先想象的要好
-
有时 cookie 是整个会话,有时它只是一个引用存储在服务器端的东西的 ID。在任何一种情况下,cookie 都会被加密以防止篡改。
标签: ruby-on-rails session cookies web google-chrome-devtools