All Lessons / Link To Current Page With Params

Link To Current Page With Params

#270 ยท November 8, 2018

Your Teacher | Chris Oliver

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

Discussion

Loading...
logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.