From 76e111395c2b8ae4f71000fd346858c3dc899fd7 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Fri, 27 Nov 2020 10:59:24 -0600 Subject: [PATCH] Always render Action Text HTML with :html format Since #40222, Action Text HTML is rendered in the context of the current request. This causes the Action Text template format to default to the request format, which prevents the template from being resolved when the request format is not `:html` (e.g. `:json`). Therefore, override the template format to always be `:html`. Fixes #40695. --- actiontext/lib/action_text/content.rb | 2 +- actiontext/test/dummy/app/views/messages/show.json.erb | 5 +++++ actiontext/test/integration/controller_render_test.rb | 10 ++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 actiontext/test/dummy/app/views/messages/show.json.erb diff --git a/actiontext/lib/action_text/content.rb b/actiontext/lib/action_text/content.rb index 6126a4de49..ab8fc05c8c 100644 --- a/actiontext/lib/action_text/content.rb +++ b/actiontext/lib/action_text/content.rb @@ -84,7 +84,7 @@ module ActionText end def to_rendered_html_with_layout - render partial: "action_text/content/layout", locals: { content: self } + render partial: "action_text/content/layout", formats: :html, locals: { content: self } end def to_s diff --git a/actiontext/test/dummy/app/views/messages/show.json.erb b/actiontext/test/dummy/app/views/messages/show.json.erb new file mode 100644 index 0000000000..7db887350a --- /dev/null +++ b/actiontext/test/dummy/app/views/messages/show.json.erb @@ -0,0 +1,5 @@ +{ + "id": <%= @message.id %>, + "subject": "<%= j @message.subject %>", + "content": "<%= j @message.content %>" +} diff --git a/actiontext/test/integration/controller_render_test.rb b/actiontext/test/integration/controller_render_test.rb index 2e8812ed88..b731af4cb3 100644 --- a/actiontext/test/integration/controller_render_test.rb +++ b/actiontext/test/integration/controller_render_test.rb @@ -14,6 +14,16 @@ class ActionText::ControllerRenderTest < ActionDispatch::IntegrationTest end end + test "renders as HTML when the request format is not HTML" do + blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpg") + message = Message.create!(content: ActionText::Content.new.append_attachables(blob)) + + host! "loocalhoost" + get message_path(message, format: :json) + content = Nokogiri::HTML::DocumentFragment.parse(response.parsed_body["content"]) + assert_select content, "img:match('src', ?)", %r"//loocalhoost/.+/racecar" + end + test "resolves partials when controller is namespaced" do blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpg") message = Message.create!(content: ActionText::Content.new.append_attachables(blob))