Skip to content

Fix search_field raising NameError when passed autosave: true - #58336

Merged
fatkodima merged 1 commit into
rails:mainfrom
hammadxcm:test-search-field-autosave-coverage
Aug 1, 2026
Merged

fatkodima merged 1 commit into
rails:mainfrom
hammadxcm:test-search-field-autosave-coverage

Conversation

@hammadxcm

Copy link
Copy Markdown
Contributor

Summary

search_field documents an autosave: true form that resolves to the reversed request host:

# request.host returns "www.example.com"
search_field(:user, :name, autosave: true)
# => <input autosave="com.example.www" id="user_name" name="user[name]" results="10" type="search" />

That form has never worked. ActionView::Helpers::Tags::SearchField#render does:

options["autosave"] = request.host.split(".").reverse.join(".")

but Tags::Base neither defines request nor delegates missing methods to the view, so every call raises:

NameError: undefined local variable or method 'request' for an instance of ActionView::Helpers::Tags::SearchField

This isn't context-dependent — the tag object has no request in any context, so the documented example raises for everyone. I found it while writing coverage for the autosave branch, which turned out to be entirely untested; the two existing search_field tests only cover the plain call and onsearch: true.

Fix

Read the request from the view context, which is what the tag already holds:

options["autosave"] = @template_object.request.host.split(".").reverse.join(".")

Tests

Added coverage for the whole autosave branch:

  • autosave: true resolves to the reversed host and defaults results to 10.
  • An explicit autosave string is passed through untouched, still defaulting results.
  • An explicit results: is not overridden.
  • autosave: false renders autosave="false" and omits results, matching the documented output.

actionview — form_helper_test.rb, form_with_test.rb, form_options_helper_test.rb, form_tag_helper_test.rb: 832 runs, 1022 assertions, 0 failures, 0 errors. RuboCop clean.

Comment thread actionview/CHANGELOG.md Outdated
@@ -1,3 +1,17 @@
* Fix `search_field` raising `NameError` when passed `autosave: true`.

`ActionView::Helpers::Tags::SearchField#render` called `request` on the tag

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reader don't need to know these internal details. Can you delete them?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call — trimmed it down to just the one-line summary, matching the surrounding entries. Thanks for the review!

`Tags::SearchField#render` resolves a `true` autosave value to the
reversed request host:

    options["autosave"] = request.host.split(".").reverse.join(".")

But `Tags::Base` does not define `request` and does not delegate missing
methods to the view, so this raised

    NameError: undefined local variable or method 'request'

for every caller, in every context. The `autosave: true` form is
documented on `search_field` with an expected output of
`autosave="com.example.www"`, so the documented behavior was unreachable.

Read the request from the view context instead, and add coverage for the
autosave branch: a `true` value, an explicit string, `results` not being
overridden when given, and `false` omitting `results` entirely.
@hammadxcm
hammadxcm force-pushed the test-search-field-autosave-coverage branch from 43581b9 to 1d4780d Compare August 1, 2026 20:24
@fatkodima
fatkodima merged commit c31f606 into rails:main Aug 1, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants