2012-06-13 87 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