Skip to content

Commit

Permalink
deprecate passing nil as model arg instead of raising ArgumentError
Browse files Browse the repository at this point in the history
  • Loading branch information
cjilbert504 committed Feb 1, 2024
1 parent c0be0a2 commit 8672817
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion actionview/CHANGELOG.md
Expand Up @@ -17,7 +17,7 @@

*Sean Doyle*

* Raise `ArgumentError` when `nil` is passed as `model:` argument value to the `form_with` method.
* Deprecate passing `nil` as value for the `model:` argument to the `form_with` method.

*Collin Jilbert*

Expand Down
4 changes: 2 additions & 2 deletions actionview/lib/action_view/helpers/form_helper.rb
Expand Up @@ -437,7 +437,7 @@ def form_for(record, options = {}, &block)

case record
when String, Symbol
model = false
model = nil
object_name = record
else
model = record
Expand Down Expand Up @@ -754,7 +754,7 @@ def apply_form_for_options!(object, options) # :nodoc:
# form_with(**options.merge(builder: LabellingFormBuilder), &block)
# end
def form_with(model: false, scope: nil, url: nil, format: nil, **options, &block)
raise ArgumentError, "The :model argument cannot be nil" if model.nil?
ActionView.deprecator.warn("Passing nil to the :model argument is deprecated and will be raise in Rails 7.3") if model.nil?

options = { allow_method_names_outside_object: true, skip_default_ids: !form_with_generates_ids }.merge!(options)

Expand Down
5 changes: 2 additions & 3 deletions actionview/test/template/form_helper/form_with_test.rb
Expand Up @@ -338,13 +338,12 @@ def url_for(object)

super
end

def test_form_with_when_given_nil_model_argument
error = assert_raises(ArgumentError) do
assert_deprecated(ActionView.deprecator) do
form_with(model: nil) do
end
end
assert_equal "The :model argument cannot be nil", error.message
end

def test_form_with
Expand Down

0 comments on commit 8672817

Please sign in to comment.