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

No way to link_to method: :patch within a form #406

Closed
creativetags opened this issue Dec 1, 2022 · 2 comments
Closed

No way to link_to method: :patch within a form #406

creativetags opened this issue Dec 1, 2022 · 2 comments

Comments

@creativetags
Copy link

My layout requires a button that links to an action with PATCH method inside another form. I can't see a way to do this anymore with Turbo. button_to inside an existing form generates a 2nd CSRF token which breaks on submit "Can't verify CSRF token authenticity". link_to tries GET instead.

I've tried:

button_to 'Verify', verify_path, method: :patch
link_to 'Verify', verify_path, method: :patch
link_to 'Verify', verify_path, data: { turbo_method: :patch }

Prior discussion 1, 2, 3

@creativetags
Copy link
Author

creativetags commented Dec 1, 2022

I found a workaround here.

Add the button_to outside the form and off canvas (class visually-hidden with bootstrap)
= button_to 'delete', method: :delete, data: { turbo_confirm: 'are you sure?' }, id: 'delete-btn'

Then where you want the delete button to appear add a label tag that points to the button:
= label_tag 'delete-btn', 'Delete', class: 'btn btn-secondary'

It's surprising the label connects with and submits the button even though it's inside a different form.

@dhh
Copy link
Member

dhh commented Jan 3, 2023

You can't have nested forms. Turbo creates a form when you use a custom method: on a link/button.

@dhh dhh closed this as completed Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants