6

i am making a ecommerce application in which the categories are visible in side bar on all pages. i wrote a method in application controller

def categories
  @categories = Category.all
end

but how can i make this method available to all controllers and actions by default so that i dont have to specifically call this method in every action

def list
  categories
  @products = Product.order('title').page(params[:page]).per(4)
end
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
imran
  • 626
  • 2
  • 13
  • 24
  • define that method in application_controller and after application controller definition add `helper_method :categories` (what you want is called `helper_method`) – cristian Jan 09 '12 at 13:16

4 Answers4

11

You can define your method in application_controller.rb and since every other controller inherits from this one, that method will be available to all the controllers. Also, to make it a helper method for it to be available in the views, you can say helper_method :my_method in the application_controller.rb.

Now, for it to be automatically evaluated before any other action in a controller, you can use a before_filter. Add before_filter :my_method in the controller you want this method to be evaluated before any action or in the application_controller.rb

Syed Aslam
  • 8,707
  • 5
  • 40
  • 54
0

Maybe the most proper way is to use gem cells. Definitely should try: http://cells.rubyforge.org/

-1

If you want something that has to be across the entire application, like in this case : @categories , then write a helper.

in app/helpers/application_helper.rb

module ApplicationHelper
  def get_all_categories
     @categories = Category.all
  end
end
Kaushik Thirthappa
  • 1,041
  • 2
  • 9
  • 21
-2

I dont thing that there is a way to run this global. You could run it in a before_filter so its called before each action but you must specify this in every controller again!

My Suggestion would be to put the category sidebar stuff in a partial which contains a Call like this:

<% Category.all.each do |category| %>
     <% do stuff to display all categories %>
<% end %>

Then you could simply add the render call to your layout/application.html.erb that would do it for you!

davidb
  • 8,884
  • 4
  • 36
  • 72
  • 1
    `You could run it in a before_filter so its called before each action but you must specify this in every controller again` ... If you place a `before_filter` in the ApplicationController, you don't have to specify it again in every controller action (since by default they inherit the filters). Also, you're doing model calls in the views which is never a good thing. Go easy on throwing the f word around. – iwasrobbed Jan 09 '12 at 13:25
  • Its not a problem to do a `all` call in the view its okay to call Methods on the model a long as there is no logic in it which belongs to the `model` or the `controller`. – davidb Jan 09 '12 at 13:35
  • I think something like this definitely belongs in the view, possibly in a helper. I'd add <% cache('sidebar_categories') do %> around it and have an update to a sidebar category invalidate this. – sunkencity Jan 10 '12 at 13:08