Reputation: 81
I'm probably doing something really stupid but i'm unsure what i'm doing wrong. I'm making a counter that sees how many times the user has been on the index page in their current session.
The following is in a 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
And the following here is in application.html.erb layout view.
<%= @counter_msg %>
Of course with other code but that seems irrelevant for now.
Nothing at all is displayed from @counter_msg
What am i doing wrong? Thanks.
Upvotes: 1
Views: 47
Reputation: 2779
It's looking like you are calling method in wrong place, if you want to show @counter_msg
then it should be defined inside application controller first include helper
include ActionView::Helpers::TextHelper
into controller
also, the current code is telling you can use your variable inside store index
page.
Upvotes: 0
Reputation: 376
pluralize is a helper method. You must use the line bellow in application.html.erb
<%= "You've visited this page #{pluralize(@counter, "time")}" %>
or, include helper in your controller:
include ActionView::Helpers::TextHelper
Upvotes: 3
Reputation: 976
The pluralize method is a view helper and should be called from inside the view. Also views are exactly designed for this purpose so a display string should be in the view anyway.
<%= "You've visited this page #{pluralize(@counter, "time")}" %>
Delete the @counter_msg line from the controller.
Upvotes: 0