Question

Je voudrais centraliser des actions similaires de certains contrôleurs et avoir écrit un contrôleur à partir duquel les autres contrôleurs héritent. Cela fonctionne bien.

# calling Configurations#index will render configurations/index.html.erb
# while 'configurations' being the internal controller_path used to look for the view
class ConfigurationsController < EditorController
end

class EditorController < ApplicationController
 def index
  render 'index'
 end
end

Mais maintenant, je voudrais centraliser les vues sur la "base"-contrôler, donc si un contrôleur hérité est appelé, le contrôleur_path utilisé devrait être le contrôleur de base.

Existe-t-il un moyen de réécrire un nom de contrôleurs ou un contrôleur_path?

J'ai regardé la source d'AbstractController :: Base et j'ai trouvé que (ligne 90)

def controller_path
  @controller_path ||= name.sub(/Controller$/, '').underscore unless anonymous?
end

J'ai donc juste besoin de définir @Controller_Path de mon contrôleur de base, n'est-ce pas? Cela ne change rien:

#just does the same as above
class EditorController < ApplicationController
 @controller_path = 'editor'
 def index
  render 'index'
 end
end

Y a-t-il donc un moyen de définir manuellement le contrôleur_path?

Super merci d'avance!

Était-ce utile?

La solution

putain je l'ai trouvé seul!

Je viens de remplacer la méthode Controller_Path:

class EditorController < ApplicationController
 def controller_path
  'editor'
 end
 #...
end

Cela utilisera jamais le «rédacteur» de vue de vue pour tout contrôleur héritant.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top