> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/link-to-the-current-page-with-query-params.md).

# Link To The Current Page With Query Params

The `link_to` method is an ActionView helper for generating an `a` tag within a Rails view. There are two arguments that tend to comprise this method: the link text and a path helper.

```ruby
<%= link_to 'Home', root_path %>
```

The `link_to` method can be used to generate a link to the current page with query params. You can do this be providing a hash instead of a path helper as the second argument.

```ruby
<%= link_to "All", {filter: 'all'} %>
<%= link_to "New", {filter: 'new'} %>
<%= link_to "Posted", {filter: 'posted'} %>
```

The hash can contain one or more key-value pairs which will be turned into query params and appended to the end of the current base path.

If these are part of the `posts` index page, then they will render as:

```html
<a href="/posts?filter=all">All</a>
<a href="/posts?filter=new">New</a>
<a href="/posts?filter=posted">Posted</a>
```

This is a great way to create links for a Rails action that presents different data based on query params. Often this is an index page where filtering is needed.

[source](https://gorails.com/episodes/rails-link-to-current-page-with-params)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/link-to-the-current-page-with-query-params.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
