Blankman
Blankman

Reputation: 267310

How to add querystring values to a link_to path

I have a link to that looks like:

<%= link_to "CSV", admin_report_user_path(@user.id), format: "csv") %>

I want to add some query string values to the link, how can I add them?

Upvotes: 1

Views: 294

Answers (1)

fool-dev
fool-dev

Reputation: 7777

You can add this using comma like below

<%= link_to "CSV", admin_report_user_path(@user.id, query2, query3), format: "csv") %>

If you need to add with key like below

<%= link_to "CSV", admin_report_user_path(@user.id, foo: "bar", baz: "quux"), format: "csv") %>

If you need to merge query string with current URL parameters like below

#=> www.example.com/search?query=rails # current URL
<%= link_to 'CSV', request.query_parameters.merge(foo: "bar", baz: "quux") %>
#=> www.example.com/search?query=rails&foo=bar&baz=quux # after merge

link_to can also produce links with anchors

<%= link_to "CSV", user_path(@user, anchor: "wall") %>
# => <a href="/users/1#wall">User wall</a>

Use _blank to open a link in a new window.

<%= link_to "CSV", admin_report_user_path(@user.id, foo: "bar", baz: "quux"), target: "_blank" %>
# => <a href="user/report?id=1&foo=bar&baz=quux" target="_blank">CSV</a>

Hope it helps

Upvotes: 3

Related Questions