在控制器中,我称之为服务:
MyService.call
Run Code Online (Sandbox Code Playgroud)
在MyService.call
方法中我想使用url帮助器:
Rails.application.routes.url_helpers.something_url
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
Run Code Online (Sandbox Code Playgroud)
在config/environments/development.rb我有:
config.action_mailer.default_url_options = { host: 'localhost:3000' }
config.action_controller.default_url_options = { host: 'localhost:3000' }
Run Code Online (Sandbox Code Playgroud)
我该怎么设置不得错误?
Nob*_*ita 11
您可以将配置文件中的主机设置为:
Rails.application.routes.default_url_options[:host] = 'your_host'
这将设置默认主机不只是for action_mailer
和action_controller
,而是用于任何使用url_helpers
.
根据这一线索,利用航线助手时,他们没有延迟default_url_options
到ActionMailer
或ActionController
配置,预计将有一个default_url_options
在类方法。这对我有用:
class MyService
include Rails.application.routes.url_helpers
def call
something_url
end
class << self
def default_url_options
Rails.application.config.action_mailer.default_url_options
end
end
end
Run Code Online (Sandbox Code Playgroud)
请注意,您可以使用action_mailer
或action_controller
配置,具体取决于配置的对象。
归档时间: |
|
查看次数: |
6707 次 |
最近记录: |