Dynamically inserting params in link_to

Hey …

I have a button_to tag like this in my views:

<%= button_to report_filter_values_path(@report, @filter), data: {"filter-target"=>"searchOutput", "action" =>"click->filter#searchClicked"} do %>
<% end %>

The button text is added dynamically via a stimulus controller. Basically, its a modification on something a user searched on a search box on the same page.

fetchSearch(event) {
  var searchQueryTarget = this.searchQueryTarget.value
    if(searchQueryTarget !== "") {
      var searchString = "Filters on '" + searchQueryTarget + "'"
      this.searchOutputTarget.textContent = searchString

So, in the above Stimulus controller method, I am setting the button text to the value from searchString. This works perfectly… But now I also need to pass this button text value in the url as params. So, my url should be updated from report_filter_values_path(@report, @filter) to something like this: report_filter_values_path(@report, @filter, content: "searchStringValue"). I need this value in my controller, so thought I could pass that as part of the params.

I’m thinking this can be done if I modify the href attribute. But stuck on this approach. If you have any suggestions, please share.

Thank you! :slight_smile:


I put the data attributes on the form element by updating this:

<%= button_to report_filter_values_path(@report, @filter), data: {"filter-target"=>"searchOutput", "action" =>"click->filter#searchClicked"} do %>
<% end %>


<%= button_to report_filter_filter_values_path(@report, @filter), form: {data: {"filter-target"=>"searchOutput", "action" =>"click->filter#searchClicked"}} do %>
                  <% end %>

So, the form now looks like this:

<form data-filter-target="searchOutput" data-action="click->filter#searchClicked" class="button_to" method="post" action="/reports/paid-sales-over-time/filters/1/filter_values">Filters on 'order'</form>

I need to somehow update the form action and add the params to it.

Solved this by converting the button_to tag into a form, and adding a hidden filed.
Then I updated the hidden fields value via Stimulus.