I recently added Hotwire to my rails app to update modals but this broke a link_to I had that redirected users to stripe. I looked online for ways to work around it but since Hotwire is relatively new, I couldn't find it. Does anyone have any recommendations on getting link_to to work with turbo stream?
5 Answers
There are three ways (at least) to restore default (non-Turbo) link behavior.
1: Set the data-turbo attribute to false.
<%= link_to "Click Here", path_helper_method, data: { turbo: false } %>
(or in plain html)
<a href="" data-turbo="false">
2: Set the target attribute.
<%= link_to "Click Here", path_helper_method, target: "_top" %>
(or in plain html)
<a href="" target="_top">
3: Move the link outside any Turbo frame. Any link inside a Turbo frame, without one of the above attributes, will be handled by Turbo by default (often with unexpected results).

- 1,627
- 17
- 27
If there is a link inside a turbo_frame_tag
you can make it work as usual by implementing a special frame called _top
that represents the whole page. Just add data: { turbo_frame: "_top" }
to your link.
link_to "string", object, data: { turbo_frame: "_top" }

- 1,794
- 1
- 16
- 23
The helpers that turn link_to into remote invocations will not currently work with Turbo. Links that have been made remote will not stick within frames nor will they allow you to respond with turbo stream actions. The recommendation is to replace these links with styled button_to, so you'll flow through a regular form, and you'll be better off with a11y compliance.
@edit use target: "_top" on the turbo_frame_tag to enable links within it

- 17
- 4

- 378
- 1
- 4
- 22
-
Feel free to share the code from your view of the specific link. – VegaStudios May 18 '21 at 22:29
If the link is inside of a turbo_frame_tag
with a turbo: true
or turbo: true
is set globally/on any of the parent elements, the link will act as turbo: true
meaning it won't redirect you as it will try doing an ajax request. Try doing <%= link_to 'Woodoo', root_path, data: { turbo: false } %>

- 17
- 4
I faced the same problem but
I change this
<%= link_to "Create A Comment", new_model_post_comment_path(@model_post.id),method: :get%> </span>
to this and it worked for me
<%= link_to "Create A Comment", new_model_post_comment_path(@model_post.id)%> </span>

- 3,181
- 23
- 27

- 1
- 1