Ruby on rails 如何使用RubyonRails3实现名称空间中的类继承?

Ruby on rails 如何使用RubyonRails3实现名称空间中的类继承?,ruby-on-rails,ruby,class,inheritance,namespaces,Ruby On Rails,Ruby,Class,Inheritance,Namespaces,在我的RoR3应用程序中,我有一个名为NS1的命名空间,因此我有以下文件系统结构: 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 我希望'ns_controll

在我的RoR3应用程序中,我有一个名为NS1的命名空间,因此我有以下文件系统结构:

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
我希望'ns_controller.rb'继承自应用程序控制器,因此在'ns_controller.rb'文件中我有:

class Ns::NsController < ApplicationController
  ...
end
@profile
是一个活动记录:

@profile.find(1).name
=> "Ruby on"
@profile.find(1).surname
=> "Rails"
application\u controller.rb
中,我有:

namespace "ns" do
  resources :profiles
end
class ApplicationController < ActionController::Base
  @profile = Profile.find(1)
end
class Ns::NsController < ApplicationController
  @name = @profile.name
  @surname = @profile.surname
end


<代码>@name和
@namite
变量未设置为什么?

除非这里没有显示某些代码,否则您试图在类主体中设置实例变量,而不是实例方法,这意味着该变量在控制器操作(即实例方法)中不可用

如果要查找可继承的方法,可以执行以下操作:

class ApplicationController < ActionController::Base
  def load_profile
    @profile = Profile.find(params[:id])
  end
end

class Ns::NsController < ApplicationController
  before_filter :load_profile

  def show
    # @profile assigned a value in load_profile
  end
end
class ApplicationController