gpt4 book ai didi

ruby-on-rails - 如何在 rake 上设置默认主机?

转载 作者:行者123 更新时间:2023-12-04 05:40:07 25 4
gpt4 key购买 nike

我想在 rake 任务中获取渲染字符串。但是像 new_book_url 这样的 url 没有 host..

# on rake
controller = ApplicationController.new.tap do |controller|
controller.request = ActionDispatch::Request.new({})
end
p controller.render_to_string('books/index', layout: nil) #=> "<a href=\"http://:/books/new\">New Book URL</a>\n"
p Rails.application.config.action_mailer[:default_url_options] #=> {:host=>"localhost", :port=>3000}
p Rails.application.routes.default_url_options #=> {}

# books/index
<%= link_to 'New Book URL', new_book_url %>

当我设置 Rails.application.routes.default_url_options = Rails.application.config.action_mailer[:default_url_options] 时,它不起作用...

最佳答案

您需要添加 default_url_options 作为 Controller 的实例方法:

controller = ApplicationController.new.tap do |controller|
controller.request = ActionDispatch::Request.new({})
def controller.default_url_options
host = {'production' => 'production.example.org', 'development' => 'development.example.org'}[Rails.env]
{host: host}
end
end
p controller.render_to_string('books/index', layout: nil)

关于ruby-on-rails - 如何在 rake 上设置默认主机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com