2011-01-11 9 views
0

In meiner RoR3 Anwendung habe ich einen Namensraum NS1 genannt, so dass ich diese Dateisystemstruktur haben:Wie ist es möglich, die Klassenvererbung in Namespaces mit Ruby on Rails 3?

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 

Ich möchte, dass 'ns_controller.rb' von Anwendungscontroller erbt, so in "ns_controller.rb Datei-I haben:

class Ns::NsController < ApplicationController 
    ... 
end 

Ist das der richtige Ansatz? Wie auch immer, wenn ich in dieser Situation ...


In ROOT_RAILS/config/routes.rb ich habe:

namespace "ns" do 
    resources :profiles 
end 

@profile ein Active ist:

@profile.find(1).name 
=> "Ruby on" 
@profile.find(1).surname 
=> "Rails" 

In application_controller.rb ich habe:

class ApplicationController < ActionController::Base 
    @profile = Profile.find(1) 
end 

In ns_controller.rb ich habe:

class Ns::NsController < ApplicationController 
    @name = @profile.name 
    @surname = @profile.surname 
end 

... @name und @surname Variablen nicht gesetzt sind. Warum?

Antwort

1

Wenn Sie hier nicht Code anzeigen, versuchen Sie, eine Instanzvariable in einem Klassenrumpf anstelle einer Instanzmethode festzulegen. Dies bedeutet, dass die Variable nicht in Controlleraktionen verfügbar ist (dh Instanzmethoden).

Wenn Sie Methode wollen finden, die vererbt werden kann, könnten Sie so etwas tun:

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 
Verwandte Themen