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

Add docs for data-turbo-method and data-turbo-confirm for link_to #47505

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions actionview/lib/action_view/helpers/url_helper.rb
Expand Up @@ -173,6 +173,28 @@ def _filtered_referrer # :nodoc:
# link_to "External link", "http://www.rubyonrails.org/", target: "_blank", rel: "nofollow"
# # => <a href="http://www.rubyonrails.org/" target="_blank" rel="nofollow">External link</a>
#
# ==== Turbo
#
# Rails 7 ships with Turbo enabled by default. Turbo provides the following +:data+ options:
#
# * <tt>turbo_method: symbol of HTTP verb</tt> - Performs a Turbo link visit
# with the given HTTP verb. Forms are recommended when performing non-+GET+ requests.
# Only use <tt>data-turbo-method</tt> where a form is not possible.
#
# * <tt>turbo_confirm: "question?"</tt> - Adds a confirmation dialog to the link with the
# given value.
#
# {Consult the Turbo Handbook for more information on the options
# above.}[https://turbo.hotwired.dev/handbook/drive#performing-visits-with-a-different-method]
#
# ===== \Examples
#
# link_to "Delete profile", @profile, data: { turbo_method: :delete }
Copy link
Member

Choose a reason for hiding this comment

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

This example should probably be a button_to. Is there a more appropriate example we can use here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, @ghiculescu ! I've added an expanded example of button_to that fails, and a short explanation on why link_to with data-turbo-method should be used instead in this commit: 1e568cd

# # => <a href="/profiles/1" data-turbo-method="delete">Delete profile</a>
#
# link_to "Visit Other Site", "https://rubyonrails.org/", data: { turbo_confirm: "Are you sure?" }
# # => <a href="https://rubyonrails.org/" data-turbo-confirm="Are you sure?">Visit Other Site</a>
#
# ==== Deprecated: \Rails UJS Attributes
#
# Prior to \Rails 7, \Rails shipped with a JavaScript library called <tt>@rails/ujs</tt> on by default. Following \Rails 7,
Expand Down Expand Up @@ -224,9 +246,6 @@ def link_to(name = nil, options = nil, html_options = nil, &block)
# Generates a form containing a single button that submits to the URL created
# by the set of +options+. This is the safest method to ensure links that
# cause changes to your data are not triggered by search bots or accelerators.
# If the HTML button does not work with your layout, you can also consider
# using the +link_to+ method with the <tt>:method</tt> modifier as described in
# the +link_to+ documentation.
#
# You can control the form and button behavior with +html_options+. Most
# values in +html_options+ are passed through to the button element. For
Expand All @@ -240,6 +259,10 @@ def link_to(name = nil, options = nil, html_options = nil, &block)
# The form submits a POST request by default. You can specify a different
# HTTP verb via the +:method+ option within +html_options+.
#
# If the HTML button generated from +button_to+ does not work with your layout, you can
# consider using the +link_to+ method with the +data-turbo-method+
# attribute as described in the +link_to+ documentation.
Copy link
Member

Choose a reason for hiding this comment

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

The link_to documentation does not describe data-turbo-method yet or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This links back to the documentation I added for data-turbo-method in the link_to docs. We did the same previously for the :method argument so I thought of doing the same for data-turbo-method. However, I'm happy to remove it if we feel it's not necessary as well!

#
# ==== Options
# The +options+ hash accepts the same options as +url_for+. To generate a
# <tt><form></tt> element without an <tt>[action]</tt> attribute, pass
Expand Down