Ruby on Rails会话增加2

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

class StoreController 5 end end 

我的观点是

 

..

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

你可以在动作中调用increment_counter两次:首先设置@count,然后再次设置@shown_message。

作为对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 end 

您已经有了答案,但是对于最佳实践,您应该让@shown_message到视图来完成工作,即使使用if控件也是如此。 也可以直接使用控制器中的@count到视图。

像这样的东西:

 

<%= "You've been here: #{@count} times" if @count > 5 %>

..

非常好的书,我正在使用它!