From 9febb318043e5320d7338f0267c6aef651cd9315 Mon Sep 17 00:00:00 2001 From: Alberto Almagro Sotelo Date: Tue, 9 Oct 2018 23:10:07 +0200 Subject: Prevent rich_text_area from raising when inexistent attribute is passed (#24) When an inexistent attribute is passed as a parameter to currently existing Rails' Form Helpers they render an empty tag instead of raising an exception. This commit confers the same behavior to `rich_text_area`. Fixes #19 --- app/helpers/action_text/tag_helper.rb | 2 +- test/template/form_helper_test.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/template/form_helper_test.rb diff --git a/app/helpers/action_text/tag_helper.rb b/app/helpers/action_text/tag_helper.rb index 103b65eb97..c7ba416880 100644 --- a/app/helpers/action_text/tag_helper.rb +++ b/app/helpers/action_text/tag_helper.rb @@ -45,7 +45,7 @@ module ActionView::Helpers end def editable_value - value.body.try(:to_trix_html) + value&.body.try(:to_trix_html) end end diff --git a/test/template/form_helper_test.rb b/test/template/form_helper_test.rb new file mode 100644 index 0000000000..d7bdbad26c --- /dev/null +++ b/test/template/form_helper_test.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'test_helper' + +class ActionText::FormHelperTest < ActionView::TestCase + tests ActionText::TagHelper + + def form_with(*) + @output_buffer = super + end + + test "rich_text_area doesn't raise when attributes don't exist in the model" do + assert_nothing_raised do + form_with(model: Message.new, scope: :message, id: "create-message") do |form| + form.rich_text_area(:not_an_attribute) + end + end + + assert_match "message[not_an_attribute]", output_buffer + end +end -- cgit v1.2.3