Set default_url_options auf initialize
-
30-09-2019 - |
Frage
Ich brauche den Host in einen der Umgebungen in meiner Rails-Anwendung zu erzwingen.
Ich habe die Überschreibung an der Arbeit, indem
def default_url_options(opts={})
opts.merge({:host => 'stg.my-host.com'})
end
in app / controllers / application.rb
Aber gibt es eine Möglichkeit, dies auf initialize einzustellen, vorzugsweise in einer config / Umgebungen / ... Datei? Ich mag aus dem Controller bedingte env Logik halten.
Aber wenn ich versuche
config.action_controller.default_url_options = { ... }
oder auch
ActionController::Base.default_url_options = { ... }
I "nicht definierte Methode" erhalten, selbst wenn eine Verpackung in einem config.after_initialize {...}
irgendwelche Gedanken?
Lösung
Die Antwort ist ... es ist unmöglich, weil default_url_options als Funktion implementiert ist, nicht ein attr.
Von action_pack / action_controller / base.rb: 1053:
# Overwrite to implement a number of default options that all url_for-based methods will use. The default options should come in
# the form of a hash, just like the one you would use for url_for directly. Example:
#
# def default_url_options(options)
# { :project => @project.active? ? @project.url_name : "unknown" }
# end
#
# As you can infer from the example, this is mostly useful for situations where you want to centralize dynamic decisions about the
# urls as they stem from the business domain. Please note that any individual url_for call can always override the defaults set
# by this method.
def default_url_options(options = nil)
end
Andere Tipps
Sie können die Konfiguration auf diese Weise erzwingen:
config.action_mailer.default_url_options = { :host => "foo.com" }
Das Problem in Ihrem Code ist, dass Sie config.action_controller
verwendet haben, statt config.action_mailer