Step to reproduce:
rails new testurl
cd testurl
resources :tests, only: [:index]
in config/routes.rb<%= tests_url %>
.What I need to do is render this view as HTML string, to do that I did the following:
rails c
ApplicationController.render(template: 'test/test')
Running the code above gives me the following result:
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"
As you can see <%= tests_url %>
is rendered as http://example.org/tests
. Where is this http://example.org
defined? And how to replace it?
I tried searching example.org
in a new rails app and found the following piece of code in 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
I tried uncommenting the code and replaced it with the following:
# 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
Then after that, I restarted the app and rails console.
But still running ApplicationController.render(template: 'test/test')
still gives me http://example.org
where I expect it to be https://google.com
. What seems to be the problem?
Environment:
I haven't found out how to change example.org
globally but the solution below works enough for me.
renderer = ApplicationController.renderer.new(
http_host: 'google.com',
https: true
)
renderer.render(template: 'test/test')
Now it 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"
It now replaces http://example.com
with https://google.com
.