DavyJonesLocker / client_side_validations

Client Side Validations made easy for Ruby on Rails
MIT License
2.69k stars 404 forks source link

Wrong HTTP method after updating from 19.1.1 to 20.0.0 #867

Closed lucasfais closed 2 years ago

lucasfais commented 2 years ago

Steps to reproduce

We have a rails app using client_side_validations and we had problems updating it from to 19.1.1 to 20.0.0.

A few forms using client_side_validations are issuing the request with the wrong HTTP method, resulting in a 404.

For example, action defined as PATCH, but POST is used.

Our forms are defined with simple_form_for and we are specifying the correct http method via method: argument.

I noticed that, on 20.0.0, <input type="hidden" name="_method" value="patch"> is missing from the HTML, so rails doesn't know it is a PATCH method.

Could this be related to https://github.com/DavyJonesLocker/client_side_validations/pull/862 mentioned in the CHANGELOG? Is that something I should adjust on my code?

Expected behavior

Requests coming from JS should use the correct HTTP method.

Actual behavior

Requests coming from JS are using a wrong HTTP method.

System configuration

Rails version: 6.1.4.4

Ruby version: 2.7.4

Client Side Validations version: 20.0.0

Code snippet from your model of the validations

Not relevant; it happens regardless of which validations are being used

The whole form code from your template

= simple_form_for @model, url: my_action_path, remote: true, validate: true, method: :patch do |f|

The resulting HTML

Regular form element, but <input type="hidden" name="_method" value="patch"> is missing.

Browser's development console output

Additional JavaScript Libraries

n/a

Repository demostrating the issue

n/a

tagliala commented 2 years ago

Hi,

Thanks for this report, I can confirm.

I've identified the issue and I do not recommend to use CSV with Rails 7.

There is something wrong and I do not have a clear clue on both how to fix this behavior and why the tests didn't find the bug.

The problem is here:

https://github.com/DavyJonesLocker/client_side_validations/blob/8207046e4d98fa055a20a7f80101e200122fcdd1/lib/client_side_validations/action_view/form_builder.rb#L32-L33

On localhost, I cannot even add validate: true to a basic form_with and I get

undefined method `content_tag' for ActionView::Base:Class

without a backtrace

As a workaround, if CSV works for your use case, ~please manually add the hidden field with the method to the form element~ pass the method in html options:

= simple_form_for @model, url: my_action_path, remote: true, validate: true, html: { method: :patch } do |f|

PR (and a failing spec) are very welcomed

tagliala commented 2 years ago

20.0.1 released, thanks

lucasfais commented 2 years ago

Awesome. I'm actually using rails 6.1.4.4. I'll test with the new gem version. Thanks!

lucasfais commented 2 years ago

I can confirm 20.0.1 fixes the issue with rails 6.1.4.4. Thank you!

tagliala commented 2 years ago

I've splitted the other issue I've found while investigating this into #869

I've also understood why the tests pass. The other issue is related to Rails 7, so it should not be a problem on 6.1