我想在rake任务上获得渲染字符串。但像new_book_url
这样的网址没有主机..
# 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]
时,它不起作用......
答案 0 :(得分:0)
您需要添加default_url_options
作为控制器的实例方法:
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)