Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

helper for all views in a namespace

Is there a way to create a view helper file that will be available to all views in a namespace? Like application_helper.rb, but only working for a given namespace.

Specifically, I have a namespace called "office". I want to set up a view helper that is accessible to any view within the "office" namespace.

Thanks.

like image 256
Brett Avatar asked Jul 27 '11 20:07

Brett


1 Answers

I would suggest that you have a BaseController for that specific namespace. For example,

class Office::BaseController < ApplicationController
  helper :office   
end

And inherit this controller in all the other controllers within that namespace.

class Office::UsersController < Office::BaseController
  def index
      ..
  end
end

Now all the methods within the helper office_helper.rb is present within this namespace.

Also, this is a good practice to separate the concerns / code for the controller namespaces.

like image 113
hyxnat Avatar answered Oct 02 '22 02:10

hyxnat