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
end
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.

James:


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

def add_to_cart
  .
  .
  else
    @cart = find_cart
    @cart.add_product(product)
    session[:counter] = nil
  end
end

def index_access_counter
  session[:counter] ||= 0
  session[:counter] += 1
end
store/index.rhtml

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

VOLKAN:

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:

To VOLKAN:

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")}" 
    else
      object = nil
    end
  end

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