small medium large xlarge


Dave says

You can do this using an if statement

<% if @count > 5 -%>
  You've viewed this page <%= pluralize(@count,"time") %>, mon ami.
<% end -%>

Combusean says:

Isn’t the idea to display the counter ‘business logic’ and thus in the store controller

@display_count = nil

if @count > 5 
  @display_count = @count
and then in the view

<% if @display_count -%>
  You've viewed this page <%= pluralize(@display_count,"time") %>, mon ami.
<% end -%>

I know this seems kind of pedantic and this borders on unDRY, but I’m new to this and am wondering if the view is indeed the place to make the decision of whether to show the counter or not?

Dirtymafia says:

I just put

<%= "You've been here " + pluralize(@count, "time")+"." if @count > 5%>
I’m kind of a beginner but seems alright to me. But yes does seem to border on DRY or unDRY. However, I’m not sure that only viewing the count when it is greater than 5 is business logic..isn’t it more about the layout/view than anything else.

Anthony says:

I had to test for nil before checking the @count > 5…

<% if @count != nil && @count > 2 %>
  <h3>You've been here <%= pluralize( @count, 'time' ) %></h3>
<% end %>

Haig says:

Re:Combusean, No you don’t change the controller code, you just want to change the view code that displays the message.

What I think is an intuitive way:

<% unless session[:index_counter] <= 5 %>
  You've been here <%= pluralize(session[:index_counter],"time") %>
<% end %>

Martin says:

Btw: why does everybody pluralize, when we are showing the value only if it’s greater than five … :-)

Sona says:

why do come of you put:

   <% ...blah... -%>CODE<% end -%>  

That little dash there.

Moritz says:

@Sona: The dash in ‘->’ just makes the HTML output nicer. It oppresses the newline character after the ‘>’ that would be inserted into the resulting HTML code otherwise.


<%= pluralize(session[:counter], "page load") if session[:counter] > 5 -%><br />

I think the MVC way would actually be to store the 5 in a variable assigned in the controller. That way you change the “business logic” of how many page loads you want in the controller and would also validate the existence of the pluralize function so your view can seemlessly handle any changes to the “business logic”.

Also, I removed the break with the ”-%>” and then put one in with the ” ” so that it leaves a space when the phrase is not shown. Not sure which way results in a better end-user experience, but I thought it was a good proof-of-concept :)

h4. Scott says: store_controller.rb

def index
  @count = index_access_counter
  @count_minimum_to_display = true if @count > 5

def add_to_cart
    @cart = find_cart
    session[:counter] = nil

def index_access_counter
  session[:counter] ||= 0
  session[:counter] += 1

<%= "You have visited this page #{@count} times!" if @count_minimum_to_display %>


I get an error;

You have a nil object when you didn't expect it! The error occurred while evaluating nil.>

If i take the “count” bit out of the picture it works but trying to display the count > 5 gives me the nil error…

Any help would be appreciated Thanks in Advance

PojoGuy says:


count > 5 or @count > 5? You got to use @count in if-condition.

Trientalis says

Similar to Combusean I wanted to keep logic outside the view and I even tried to put the pluralize out of it. To do so, I wrote a helper method in store_helper.rb:

def count_hits(object)
   if @count > 5
      object = "You've been here #{pluralize(@count.to_i, "time")}" 
      object = nil

In the view (index) I called this method to an object, whose name does not matter:

<%=count_hits @greeter %>

Erykwalder says

I just did:
<%= 'Viewed ' + pluralize(@counter, 'time') if @counter > 5 %>

The reason why I wouldn’t put the logic for deciding whether or not to display the count in the view, is because the logic relates directly to the view.

Diogo says

I agree with Erywalder, but also that the pluralize is totally useless in this case. I did

<%= 'Viewed ' + @counter.to_s + ' times' if @counter > 5 %>
Page History
  • V23: Viacheslav Soldatov [over 3 years ago]
  • V20: Bess Fernandez [over 4 years ago]
  • V19: Bess Fernandez [over 4 years ago]
  • V18: Justin McGuire [over 4 years ago]
  • V14: Brent Merrick [almost 5 years ago]
  • V13: Joseph Venator [about 5 years ago]
  • V12: Joseph Venator [about 5 years ago]
  • V11: Joseph Venator [about 5 years ago]
  • V10: Joseph Venator [about 5 years ago]
  • V9: Joseph Venator [about 5 years ago]