My Rails 3.1 application is completely implemented inside a namespace. When I call views, this namespace is not included, so that I have to type the full qualified name of each class each time. I guess this is a bug or at least a missing feature. How can I include my namespace Aef::Newman automatically in each view context, so that I don’t have to type the full qualified name each time?
Notice that the following statement inside the HAML view template fails because the method include can not be found:
- include Aef::Newman
Both components of this namespace are of type Module. For example, my controllers are located in app/controllers/aef/newman and the application controller looks like this:
module Aef
module Newman
class ApplicationController < ActionController::Base
protect_from_forgery
end
end
end
There is a controller named HandledAddressesController which looks like this:
module Aef
module Newman
class HandledAddressesController < ApplicationController
def index
@handled_addresses = HandledAddress.all
end
end
end
end
Notice, that even the HandledAddress model is actually named Aef::Newman::HandledAddress and is located in app/models/aef/newman/handled_address.rb .
My routes.rb looks like this:
resources :handled_addresses, controller: 'aef/newman/handled_addresses, only: :index
The view template path is app/views/aef/newman/handled_addresses/index.haml.haml
Firstly, you shouldn’t really be using constants inside a template. Set some instance variables or make some helpers to do it.
But, if you really want access to your namespaced constants (classes, modules, etc) then in an initializer you could:
ActionView compiles all templates into this module.