Skip to content

ruflet_rails: hand Action View-shaped text_field, textarea and checkbox calls back to Action View (form_with works again) - #120

Merged
AdamMusa merged 4 commits into
mainfrom
audit/rails-formhelper-shadowing
Nov 21, 2026
Merged

AdamMusa merged 4 commits into
mainfrom
audit/rails-formhelper-shadowing

Conversation

@AdamMusa

Copy link
Copy Markdown
Owner

Fixes #116

The Railtie includes the HTML DSL helpers into every view of the host app, above ActionView::Helpers::FormHelper. The DSL text_field, and on Rails 8 textarea and checkbox, hid Action View's helpers of the same name. As a result, form_with, form_for and fields_for builders (f.text_field, f.text_area, f.check_box, f.textarea, f.checkbox, f.collection_check_boxes) raised ArgumentError on ordinary web pages, which returned HTTP 500.

What changed

packages/ruflet_rails/lib/ruflet/rails/html_dsl/view_helpers.rb:

  • When Ruflet::Rails::HtmlDsl::ViewHelpers is included (self.included), each DSL helper that hides a method of the including class or module is wrapped. Methods every object has (Object.ancestors, such as ruflet_core's Kernel builders) do not count, because these helpers exist to replace them in views. Working this out at include time means:
    • only real collisions are wrapped (in ActionView::Base on Rails 8.1: text_field, textarea, checkbox; on Rails 7 only text_field);
    • registry names added to ruflet_core later are covered too;
    • ActionView::Base is never loaded early.
  • In a class where the helper hides a method, a call goes to that method (super) when:
    • it has more positional arguments than the DSL signature takes (text_field more than 1, textarea more than 2, checkbox more than 0). Every FormBuilder call passes object_name, method and an options Hash; or
    • it is textarea with a Symbol second argument (Rails 8's textarea(:post, :body)). That Symbol names a model attribute, while the DSL's second argument is the text to show.
  • Every other call runs the original DSL helper body (bind_call) with the same arguments, so it renders byte-identical markup and raises the same errors.

The README notes that Action View-shaped calls still reach Action View.

How verified

  • New test/rails_form_helpers_test.rb, with test/support/rails_form_helpers_app.rb, boots a real Rails app in its own process, as rails_executor_test.rb does. ruflet_rails is required before initialize!, as Bundler.require loads it. The app is booted twice, once with ruflet_rails and once without it (WITHOUT_RUFLET=1). Through real requests it renders 19 Rails cases: form_with, form_for and fields_for with each field helper, options, custom checkbox values, collection_check_boxes, and direct text_field/textarea/checkbox/text_area/check_box. The test asserts that every case renders the same markup in both runs, and that 7 DSL calls in the same app's views render their 0.0.23 markup.
  • New test/html_dsl_hidden_helpers_test.rb uses stand-ins with Rails 8 and Rails 7 FormHelper signatures. It covers:
    • Rails-shaped calls reach the hidden helper;
    • every DSL call keeps its markup in all three view shapes;
    • Rails 7 views keep the DSL textarea/checkbox, which hide nothing there;
    • views without a hidden helper raise the same ArgumentError as before;
    • helpers that hide nothing (column, alert_dialog, input) are not wrapped.
  • Planting the bug back (reverting view_helpers.rb) makes both new files fail: 2 failures in rails_form_helpers_test.rb, and 1 failure plus 2 errors in html_dsl_hidden_helpers_test.rb. With the fix they pass. The DSL-markup tests pass both before and after, which shows that DSL output is unchanged.
  • packages/ruflet_rails: each of the 21 test files run in its own process, 0 failures. bundle exec rake test gives 315 runs, 2969 assertions, 0 failures, 0 errors, 2 skips. The 2 skips are the existing SQLite executor scenarios, same as on main.
  • H1 goldens: cd packages/ruflet && ruby test/compat/compat.rb --jobs 4 gives "checked 358 goldens", none failing.
  • The audit's standalone repros (form_with/form_for/direct helpers on a Rails 8.1.3.1 page) now return HTTP 200 with Rails' markup for every case.
  • No warnings under ruby -w. The wrapped method is removed before it is redefined.

Tested on Ruby 4.0.5 / Rails 8.1.3.1. Rails 7 has no FormBuilder#textarea, so the Rails 8-only cases are skipped there, and the Rails 7 shape is covered by the unit stand-ins.

Behavior changes

Bug fix (D6): in Rails views, calls with Action View's FormHelper shape to a DSL helper of the same name now render Action View's markup:

  • Calls that raised ArgumentError before: every f.text_field/f.text_area/f.textarea/f.check_box/f.checkbox/f.collection_check_boxes in form_with, form_for and fields_for, plus direct text_field(:post, :title, ...) and, on Rails 8, checkbox(:post, :published, ...).
  • On Rails 8, direct textarea(:post, :body, ...) (a Symbol second argument). It used to silently render the DSL tag <textarea name="post">body</textarea> and now renders Rails' <textarea name="post[body]" id="post_body">, which is what Rails 8 documents for that call. A DSL textarea's second argument is the text to show, so a Symbol there was never meaningful DSL content.

DSL calls within the DSL signature keep their path and byte-identical output. That covers every docs and test example: text_field label: ..., text_field "x", textarea "bio", "Hi", textarea :bio, "Hi", checkbox name: .... Helpers that hide nothing are not touched, and nothing changes outside Rails views.

…rea and checkbox back to Action View

The Railtie includes the HTML DSL helpers into every view, above Action
View's FormHelper. text_field (and on Rails 8 textarea and checkbox) hid
Action View's helpers of the same name, so form_with, form_for and
fields_for builders raised ArgumentError on the host app's web pages.

When the module is included, each DSL helper that hides a method of the
including class (other than methods every object has) is wrapped: a call
with more positional arguments than the DSL takes, or textarea with a
Symbol second argument, goes to the hidden helper. Every other call runs
the DSL helper unchanged.

Fixes #116
@AdamMusa
AdamMusa merged commit d3f7f12 into main Nov 21, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ruflet_rails: installing the gem breaks form_with on the host app's web pages (DSL text_field, textarea and checkbox hide Action View's FormHelper)

1 participant