سؤال

أحتاج إلى إجبار المضيف في إحدى البيئات في تطبيق Rails الخاص بي.

يمكنني الحصول على تجاوز للعمل من خلال تضمين

  def default_url_options(opts={})
   opts.merge({:host => 'stg.my-host.com'})
  end

في التطبيق/وحدات التحكم/Application.RB

ولكن هل هناك طريقة لتعيين هذا على التهيئة ، ويفضل أن يكون ذلك في ملف التكوين/البيئات/...؟ أرغب في الحفاظ على منطق ENV المشروط من وحدة التحكم.

لكن عندما أحاول

   config.action_controller.default_url_options = { ... }

او حتى

ActionController::Base.default_url_options = { ... }

أحصل على "طريقة غير محددة" ، حتى لو كان التفاف في config.after_initialize {...}

أي أفكار؟

هل كانت مفيدة؟

المحلول

الجواب ... إنه مستحيل لأنه يتم تنفيذ Default_url_options كدالة ، وليس كأرم.

من 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

نصائح أخرى

يمكنك فرض التكوين بهذه الطريقة:

config.action_mailer.default_url_options = { :host => "foo.com" }

المشكلة في الكود الخاص بك هي أنك استخدمتها config.action_controller بدلاً من config.action_mailer

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top