ruby-on-rails – 在初始化时设置default_url_options
发布时间:2020-12-17 02:59:04 所属栏目:百科 来源:网络整理
导读:我需要在我的rails应用程序中的某个环境中强制主机. 我可以通过包括使覆盖工作 def default_url_options(opts={}) opts.merge({:host = 'stg.my-host.com'}) end 在app / controllers / application.rb中 但有没有办法在初始化时设置它,最好是在config / env
我需要在我的rails应用程序中的某个环境中强制主机.
我可以通过包括使覆盖工作 def default_url_options(opts={}) opts.merge({:host => 'stg.my-host.com'}) end 在app / controllers / application.rb中 但有没有办法在初始化时设置它,最好是在config / environments / …文件中?我想将条件env逻辑保留在控制器之外. 但是,当我尝试 config.action_controller.default_url_options = { ... } 甚至 ActionController::Base.default_url_options = { ... } 我得到“未定义的方法”,即使在config.after_initialize {…}中包装 有什么想法吗? 解决方法
答案是……这是不可能的,因为default_url_options是作为函数实现的,而不是attr.
来自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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |