2012-06-13 48 views
1

我正在学习实用的书架课。我尝试制作一个会话计数器。 我的存储控制器是Ruby on Rails session [:counter]增加两个

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 

和我的看法是

<h5><p><%= @shown_message %></p></h5>.. 

,直到5倍这是行不通的。但是在它开始算作5,7,9,11之后。 。我的会话[:counter]有什么问题?

回答

8

你在你的行动呼吁increment_counter两次:在你的条件@shown_message设定@count第一的时候,再一次。

3

补充ksol答案。在上次调用中使用@count。

def index 
    @count = increment_counter 
    @products = Product.all 
    @cart = current_cart 
    @time = Time.now 
    @shown_message = "You've been here #{@count} times" if @count >5 

+0

谢谢Murifox ..This解决我的问题。 – ytsejam