views:

108

answers:

1

Hello,

I defined method 'count' in my tasks controller as:

def count
  @count = current_user.tasks.count
end

I'm not sure how to show that in my tasks views. Do I just use Tasks count: <% @count %>? How do I get in my view how many tasks the user has?

Thanks

+2  A: 

First of all, controller methods can't be called directly inside views, instead you need to use helper methods, however Rails still can help you DRY your code and declare a method in controller to to be a helper method that can be used in helpers and views. You can do that by adding this line in the body of Tasks controller:

helper_method :count

Then inside your view you can just do

<%=count%>

Btw you can redefine the count method as follows:

def count
  current_user.tasks.count
end

However I don't find a reason why you want to define a method for this in the controller. Were you I would call this directly in the view:

<%=current_user.tasks.count%>
khelll
Thanks, that works.
Senthil