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 %>

Daniel says

I agree with Erykwalder, but the hint says to do the logic in the controller. So here’s how I did it

In the controller:

session[:counter] = 0 if session[:counter].nil?
session[:counter] += 1
@counter = session[:counter] if session[:counter] > 5
And in the view:
<%= 'Visited ' + @counter.to_s + ' times.' if @counter %>

J. Venator

The hint does indeed say to consider placing the logic into the controller, and I successfully used Daniel’s code above to do so.

However, same as Erykwalder, I question whether it makes sense to do this in this particular case. Whether we create a special method to do this or not, the erb code in the view will still need to check in one form or another to see if it should present the text. Unless there’s a difference in the load this puts on the application, I think the simplest solution is to put this logic in the view. It’s not recalculating anything, but simply deciding whether to present it. Anyway, I changed it to do so in the view based on Haig’s example above. You can see that I placed it within a separate div that utilizes a special class in the CSS.
<% if notice %>
<p> id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<div class="visit_counter">
  <% unless @counter <= 5 %>
    You've visited this page <%= pluralize(@counter, 'time') %>
  <% end %>
<% @products.each do |product| %>
The code in store.css.scss…
.store {
    h1 {
        margin: 0;
        padding-bottom: 0.5em;
        font: 150% sans-serif;
        color: #226;
        border-bottom: 3px dotted #77d;

    .visit_counter {
        color: gray;
        font-style: italic;

    /* an entry in the store catalog */
    .entry {
        overflow: auto;
        margin-top: 1em;

Flo says

To all of you who are hating on the pluralize: You are all separating the view from the controller in order to not have to go into both of them in case the number ‘5’ changes but you don’t understand why you leave the pluralize? Let’s just pretend the number changes to, say .. 1. I think one should catch that upfront in case somebody might change the number in the controller to one relying on the view to manage the rest.

Page History
  • V23: Viacheslav Soldatov [almost 5 years ago]
  • V20: Bess Fernandez [over 5 years ago]
  • V19: Bess Fernandez [over 5 years ago]
  • V18: Justin McGuire [almost 6 years ago]
  • V14: Brent Merrick [about 6 years ago]
  • V13: Joseph Venator [about 6 years ago]
  • V12: Joseph Venator [about 6 years ago]
  • V11: Joseph Venator [about 6 years ago]
  • V10: Joseph Venator [about 6 years ago]
  • V9: Joseph Venator [about 6 years ago]