In my RoR3 application I have a namespace called NS1 so that I have this filesystem structure:
ROOT_RAILS/controllers/
ROOT_RAILS/controllers/application_controller.rb
ROOT_RAILS/controllers/ns/
ROOT_RAILS/controllers/ns/ns_controller.rb
ROOT_RAILS/controllers/ns/profiles_controller.rb
I would like that ‘ns_controller.rb’ inherits from application controller, so in ‘ns_controller.rb’ file I have:
class Ns::NsController < ApplicationController
...
end
Is this the right approach? Anyway if I am in this situation…
In ROOT_RAILS/config/routes.rb I have:
namespace "ns" do
resources :profiles
end
@profile is a ActiveRecord:
@profile.find(1).name
=> "Ruby on"
@profile.find(1).surname
=> "Rails"
In application_controller.rb I have:
class ApplicationController < ActionController::Base
@profile = Profile.find(1)
end
In ns_controller.rb I have:
class Ns::NsController < ApplicationController
@name = @profile.name
@surname = @profile.surname
end
… @name and @surname variables are not set. Why?
Unless there’s some code you’re not showing here, you’re trying to set an instance variable in a class body rather than an instance method, which means the variable won’t be available in controller actions (which are instance methods).
If you want find method that can be inherited, you could do something like this: