在 Rails 中生成 url 时替换 example.org
Replace example.org when generating url in Rails
重现步骤:
rails new testurl
cd testurl
- 在config/routes.rb
中添加resources :tests, only: [:index]
- 在 app/views/test/test.html.erb 中创建一个新文件,内容为
<%= tests_url %>
.
我需要做的是将此视图呈现为 HTML 字符串,为此我执行了以下操作:
rails c
- 然后运行
ApplicationController.render(template: 'test/test')
运行 上面的代码给出了以下结果:
irb(main):006:0> ApplicationController.render(template: 'test/test')
Rendering layout layouts/application.html.erb
Rendering test/test.html.erb within layouts/application
Rendered test/test.html.erb within layouts/application (Duration: 0.6ms | Allocations: 127)
Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 12.4ms | Allocations: 3390)
=> "<!DOCTYPE html>\n<html>\n <head>\n <title>Testurl</title>\n <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n <meta name=\"csrf-param\" content=\"authenticity_token\" />\n<meta name=\"csrf-token\" content=\"Cl5oXyPFrAsZl_s3m_fO1ELVkFtUajlnI30AOZ0BB9LJpMbXnGjgX1Ng_JvQhcFo3VMD67a467Cs4mjcB_ODmA\" />\n \n\n <link rel=\"stylesheet\" media=\"all\" href=\"/assets/application.debug-ead91a25923de99455378da7f1f1bb5a6839a249af27af911ec2b81709b046b7.css\" data-turbolinks-track=\"reload\" />\n <script src=\"/packs/js/application-f826770d917d8a37c1d8.js\" data-turbolinks-track=\"reload\"></script>\n </head>\n\n <body>\n http://example.org/tests\n\n </body>\n</html>\n"
如您所见,<%= tests_url %>
呈现为 http://example.org/tests
。这个 http://example.org
是在哪里定义的?以及如何替换它?
我尝试在新的 rails 应用程序中搜索 example.org
,并在 config/initializers/application_controller_renderer.rb 中找到了以下代码:
# Be sure to restart your server when you modify this file.
# ActiveSupport::Reloader.to_prepare do
# ApplicationController.renderer.defaults.merge!(
# http_host: 'example.org',
# https: false
# )
# end
我尝试取消注释代码并将其替换为以下内容:
# Be sure to restart your server when you modify this file.
ActiveSupport::Reloader.to_prepare do
ApplicationController.renderer.defaults.merge!(
http_host: 'google.com',
https: true
)
end
然后,我重新启动了应用程序和 rails 控制台。
但仍然 运行宁 ApplicationController.render(template: 'test/test')
仍然给我 http://example.org
我期望它是 https://google.com
。好像是什么问题?
环境:
- Ruby 2.7.2
- Rails6.1.3
我还没有找到如何全局更改 example.org
但下面的解决方案对我来说已经足够了。
renderer = ApplicationController.renderer.new(
http_host: 'google.com',
https: true
)
renderer.render(template: 'test/test')
现在 returns:
irb(main):016:0> renderer.render(template: 'test/test')
Rendering layout layouts/application.html.erb
Rendering test/test.html.erb within layouts/application
Rendered test/test.html.erb within layouts/application (Duration: 0.2ms | Allocations: 44)
Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 15.7ms | Allocations: 3133)
=> "<!DOCTYPE html>\n<html>\n <head>\n <title>Testurl</title>\n <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n <meta name=\"csrf-param\" content=\"authenticity_token\" />\n<meta name=\"csrf-token\" content=\"M-u_LLixMIE-SgRpRiY0I-iqZiwvg2JAnJ14Oreqb8HCoQw5s7g00qPLMPg8kmmOImj1T-tekks_zMpMf0W_mQ\" />\n \n\n <link rel=\"stylesheet\" media=\"all\" href=\"/assets/application.debug-ead91a25923de99455378da7f1f1bb5a6839a249af27af911ec2b81709b046b7.css\" data-turbolinks-track=\"reload\" />\n <script src=\"/packs/js/application-f826770d917d8a37c1d8.js\" data-turbolinks-track=\"reload\"></script>\n </head>\n\n <body>\n https://google.com/tests\n\n </body>\n</html>\n"
它现在将 http://example.com
替换为 https://google.com
。
重现步骤:
rails new testurl
cd testurl
- 在config/routes.rb 中添加
- 在 app/views/test/test.html.erb 中创建一个新文件,内容为
<%= tests_url %>
.
resources :tests, only: [:index]
我需要做的是将此视图呈现为 HTML 字符串,为此我执行了以下操作:
rails c
- 然后运行
ApplicationController.render(template: 'test/test')
运行 上面的代码给出了以下结果:
irb(main):006:0> ApplicationController.render(template: 'test/test')
Rendering layout layouts/application.html.erb
Rendering test/test.html.erb within layouts/application
Rendered test/test.html.erb within layouts/application (Duration: 0.6ms | Allocations: 127)
Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 12.4ms | Allocations: 3390)
=> "<!DOCTYPE html>\n<html>\n <head>\n <title>Testurl</title>\n <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n <meta name=\"csrf-param\" content=\"authenticity_token\" />\n<meta name=\"csrf-token\" content=\"Cl5oXyPFrAsZl_s3m_fO1ELVkFtUajlnI30AOZ0BB9LJpMbXnGjgX1Ng_JvQhcFo3VMD67a467Cs4mjcB_ODmA\" />\n \n\n <link rel=\"stylesheet\" media=\"all\" href=\"/assets/application.debug-ead91a25923de99455378da7f1f1bb5a6839a249af27af911ec2b81709b046b7.css\" data-turbolinks-track=\"reload\" />\n <script src=\"/packs/js/application-f826770d917d8a37c1d8.js\" data-turbolinks-track=\"reload\"></script>\n </head>\n\n <body>\n http://example.org/tests\n\n </body>\n</html>\n"
如您所见,<%= tests_url %>
呈现为 http://example.org/tests
。这个 http://example.org
是在哪里定义的?以及如何替换它?
我尝试在新的 rails 应用程序中搜索 example.org
,并在 config/initializers/application_controller_renderer.rb 中找到了以下代码:
# Be sure to restart your server when you modify this file.
# ActiveSupport::Reloader.to_prepare do
# ApplicationController.renderer.defaults.merge!(
# http_host: 'example.org',
# https: false
# )
# end
我尝试取消注释代码并将其替换为以下内容:
# Be sure to restart your server when you modify this file.
ActiveSupport::Reloader.to_prepare do
ApplicationController.renderer.defaults.merge!(
http_host: 'google.com',
https: true
)
end
然后,我重新启动了应用程序和 rails 控制台。
但仍然 运行宁 ApplicationController.render(template: 'test/test')
仍然给我 http://example.org
我期望它是 https://google.com
。好像是什么问题?
环境:
- Ruby 2.7.2
- Rails6.1.3
我还没有找到如何全局更改 example.org
但下面的解决方案对我来说已经足够了。
renderer = ApplicationController.renderer.new(
http_host: 'google.com',
https: true
)
renderer.render(template: 'test/test')
现在 returns:
irb(main):016:0> renderer.render(template: 'test/test')
Rendering layout layouts/application.html.erb
Rendering test/test.html.erb within layouts/application
Rendered test/test.html.erb within layouts/application (Duration: 0.2ms | Allocations: 44)
Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 15.7ms | Allocations: 3133)
=> "<!DOCTYPE html>\n<html>\n <head>\n <title>Testurl</title>\n <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n <meta name=\"csrf-param\" content=\"authenticity_token\" />\n<meta name=\"csrf-token\" content=\"M-u_LLixMIE-SgRpRiY0I-iqZiwvg2JAnJ14Oreqb8HCoQw5s7g00qPLMPg8kmmOImj1T-tekks_zMpMf0W_mQ\" />\n \n\n <link rel=\"stylesheet\" media=\"all\" href=\"/assets/application.debug-ead91a25923de99455378da7f1f1bb5a6839a249af27af911ec2b81709b046b7.css\" data-turbolinks-track=\"reload\" />\n <script src=\"/packs/js/application-f826770d917d8a37c1d8.js\" data-turbolinks-track=\"reload\"></script>\n </head>\n\n <body>\n https://google.com/tests\n\n </body>\n</html>\n"
它现在将 http://example.com
替换为 https://google.com
。