I have many controllers where I want to set up a variable @top_tier_link that has the link to the index action of those controllers. Right now I have it like this:
class School::CoursesController < ApplicationController
before_filter :set_top_tier_link
private
def set_top_tier_link
@top_tier_link = school_courses_path
end
end
class School::UsersController < ApplicationController
before_filter :set_top_tier_link
private
def set_top_tier_link
@top_tier_link = school_users_path
end
end
I would like to define one before filter that can set that up automatically for any controller. How could I achieve that?
Edit:
This is just a sample, the number of controllers needing this kind of helper is much bigger. This variable is used at the layout level, so most controllers need this.
Define it in the ApplicationController. There you can differenciate based on the
params[:controller]variable. Something like this:Or even better to specify a helper for that, which does almost the same. Like this:
UPDATE: The path names can be generated automatically if you do not want to hardcode. A bit dangerous, but it should do the work: