Link To Current Page With Params

November 8, 2018

Track your progress

Sign in to track your progress and access subscription-only lessons.

Log In

Your Teacher

Hi, I'm Chris. I'm the creator of GoRails, Hatchbox.io and Jumpstart. I spend my time creating tutorials and tools to help Ruby on Rails developers build apps better and faster.

About This Episode

Learn how to link to the current page and add, remove, or modify params in the URL. This is handy for things like search or index pages with filters.

Notes

Resources

For additional security, you should whitelist the params that you want to allow in the links.

# app/helpers/search_helper.rb
module SearchHelper
  def current_page_params
    # Modify this list to whitelist url params for linking to the current page
    request.params.slice("query", "filter", "sort")
  end
end
<%= link_to "with foo", current_page_params.merge(foo: true) %>
```erb

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.