Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

form_with default argument raises exception on Rails 8.0 #974

Closed
grantbdev opened this issue Jan 28, 2025 · 2 comments · Fixed by #975
Closed

form_with default argument raises exception on Rails 8.0 #974

grantbdev opened this issue Jan 28, 2025 · 2 comments · Fixed by #975

Comments

@grantbdev
Copy link

Steps to reproduce*

Use form_with in a view without specifying a model and without specifying validate: true:

<%= form_with(url: "/posts") do |form| %>
  <%# form stuff goes here %>
<% end %>

Here is a regression test for test/action_view/cases/test_form_with_helpers.rb:

def test_form_with_without_model_without_validation
  form_with(url: '/posts') do |f|
    concat f.text_field(:cost)
  end

  expected = whole_form_with('/posts') do
    form_field('input', id: 'cost', name: 'cost', type: 'text')
  end

  assert_dom_equal expected, output_buffer
end

Expected behavior*

The Rails fallback behavior should be used without any problem, like when the gem is not installed.

Actual behavior*

Deprecation warning on Rails 7.2:

DEPRECATION WARNING: Passing nil to the :model argument is deprecated and will raise in Rails 8.0

Exception raised on Rails 8.0+:

ArgumentError: Passed nil to the :model argument, expect an object or false

System configuration*

Rails version: 7.2+

Ruby version: 3.3

Client Side Validations version: 22.3.0


I think this problem can be solved by changing the default model option in form_with to false to match the expected Rails default:
https://github.com/DavyJonesLocker/client_side_validations/blob/v22.3.0/lib/client_side_validations/action_view/form_with_helper.rb#L7

I don't suspect there would be a backwards compatibility issue with this change.

The Rails change was initially made here: rails/rails#49943
It was changed to a deprecation warning for Rails 7.2 here: rails/rails#50931

@tagliala
Copy link
Contributor

Hi @grantbdev,

thanks for this issue

It is unfortunate that none of the existing tests is catching this or the deprecation, not even with RUBYOPT="-w"

If you want to contribute with a PR, feel free to add the test and submit it and I will take a look. Otherwise I can take a look in the upcoming days

tagliala pushed a commit that referenced this issue Jan 29, 2025
Ensure that super `form_with` is called with the right arguments.

Fix #974
tagliala added a commit that referenced this issue Jan 29, 2025
* Fix CI

* Fix: `form_with` with `validate: false` exception on Rails 8

Ensure that super `form_with` is called with the right arguments.

Fix #974

* Update changelog and version

* Enable CI

---------

Co-authored-by: Maksim <[email protected]>
@grantbdev
Copy link
Author

Thank you for the work on this @maxnovee and @tagliala!

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 a pull request may close this issue.

2 participants