Rails - link_to with only params && turbo?

in ‘regular’ rails, you can do

link_to "foo", {bar: "value"}

That links to the current url with &bar="value"

doing the same with a turbo frame fails with
No route matches {:bar=>"value"}

is there an approach to achieve this that will work?

Can you share the full ERB syntax and the HTML output?

Try url_for helper.

erb

<%= link_to "test", {bar: "value"} %>

output

<a href="http://mydomain.com/current_path?bar=value">test</a>

where the current page is http://mydomain.com/current_path

Try url_for helper.

same error.
using haml:

=link_to tag, url_for({tag: tag.to_s})

again; it works fine in the regular render, just not in the turbo render
presumably, the issue is that the turbo renderer doesn’t have access to the current request in the ‘normal’ way

I’m not sure what do you mean by a “turbo renderer”?

I’m not sure what do you mean by a “turbo renderer”?

I’m meaning when the fragment is rendered as a result of a model callback:

broadcasts_to ->(faq) { [faq.account, :faqs] }, target: "faqs"

thinking more about this - I guess it may be unavoidable.

The broadcasts callbacks are designed with the intention that they could go to any subscriber to the relevant update channel. This means there is no current url to render against as different subscribers could be on different pages.

Correct, the way you are using the callback, it does not understand which route you want to add the query parameters to. Try specifying path or controller:

link_to "foo", post_path(bar: "value")
#=> <a href="/posts?bar=value">foo</a>

link_to "foo", controller: "posts", bar: "value"
#=> <a href="/posts?bar=value">foo</a>
1 Like