Ruby on Rails会话[:counter]增加2

yts*_*jam 1 session ruby-on-rails

我正在学习务实的书架课程.我试着做一个会话计数器.我的商店控制器是

  class StoreController < ApplicationController
  def increment_counter
  if session[:counter].nil?
    session[:counter] = 0
  end
  session[:counter] += 1
end
  def index
    @count = increment_counter
    @products  = Product.all
    @cart = current_cart
    @time = Time.now
    @shown_message = "You've been here #{@count} times" if increment_counter >5
  end
end
Run Code Online (Sandbox Code Playgroud)

我的观点是

<h5><p><%= @shown_message %></p></h5>..
Run Code Online (Sandbox Code Playgroud)

直到5次它不起作用.但在它开始算作5,7,9,11之后..我的会话有什么问题[:counter]?

kso*_*sol 8

increment_counter在操作中调用两次:首先设置@count,然后再次设置为@shown_message.