在 Rails 3.2 应用程序中,我需要访问 lib
中的 url_helpers文件。我正在使用
Rails.application.routes.url_helpers.model_url(model)
但我得到
ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):
我发现了一些关于这个的东西,但没有什么能真正解释如何为多个环境解决这个问题。
即我假设我需要在我的 development.rb 和 production.rb 文件中添加一些东西,但是什么?
我见过的最接近使用
config.action_mailer.default_url_option
建议的答案,但这在 Action 邮件程序之外不起作用。
为多个环境设置主机的正确方法是什么?
请您参考如下方法:
在任何模块 Controller 中使用此字符串以使应用程序 URL-helpers 在任何 View 或 Controller 中工作。
include Rails.application.routes.url_helpers
请注意,一些内部模块 url-helpers 应该被命名空间。
示例:
根应用程序
routes.rb
Rails.application.routes.draw do
get 'action' => "contr#action", :as => 'welcome'
mount Eb::Core::Engine => "/" , :as => 'eb'
end
模块 Eb 中的 URL 助手:
users_path
添加
include Rails.application.routes.url_helpers
在 Controller
contr
所以在那个助手之后应该是
eb.users_path
所以在 Eb 模块中你可以使用
welcome_path
与根应用程序相同!