Rails应用程序视图没有显示我想要的内容

时间:2015-08-25 17:08:53

标签: ruby-on-rails

我可能正在做一些非常愚蠢的事情,但我不确定我做错了什么。 我正在创建一个计数器,用于查看用户在当前会话中访问索引页面的次数。

以下是在store_controller.rb

class StoreController < ApplicationController

 def increment_counter
 if session[:counter].nil?
   session[:counter] = 0
 end
   session[:counter] += 1
 end  

  def index
    @products = Product.order(:title)
    @counter = increment_counter
    @counter_msg = "You've visited this page #{pluralize(@counter, "time")}"

  end
end

以下是application.html.erb布局视图。

<%= @counter_msg %>

当然还有其他代码,但现在似乎无关紧要。

@counter_msg

显示什么都没有

我做错了什么? 感谢。

3 个答案:

答案 0 :(得分:3)

pluralize是一种辅助方法。您必须在application.html.erb

中使用下面的行
<%= "You've visited this page #{pluralize(@counter, "time")}" %>

或者,在控制器中包含帮助程序:

include ActionView::Helpers::TextHelper

答案 1 :(得分:0)

看起来你在错误的地方调用方法,如果你想显示@counter_msg那么它应该在应用程序控制器中首先包含帮助

include ActionView::Helpers::TextHelper

进入控制器  此外,当前代码告诉您可以在商店 index页面中使用您的变量。

答案 2 :(得分:0)

pluralize方法是一个视图助手,应该从视图内部调用。此外,视图也是为此目的而设计的,因此显示字符串无论如何都应该在视图中。

<%= "You've visited this page #{pluralize(@counter, "time")}" %>

从控制器中删除@counter_msg行。

相关问题