Low commit activity in last 3 years
A long-lived project that still receives updates
A rails controller extension for keeping specific parameters through links.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.0
 Project Readme

ParamsKeeperRails

A rails controller extension for keeping specific parameters through links.

Dependencies

  • ruby 2.3+
  • rails 5.0+

Installation

Add this line to your application's Gemfile:

gem 'params_keeper_rails'

And then execute:

$ bundle

Usage

Include modules in your controller and specify parameter keys you want to keep:

class ExamplesController < ApplicationController
  include ParamsKeeper::Controller
  keep_params :key1, :key2

Parameters are kept via url_for if destination controller is same as current controller. For example:

GET "/examples?key1=**&key2=**"

# hash arg
url_for(action: :index)  #=> '/examples?key1=**&key2=**'

# parameters are not kept if string or model arg is specified
url_for('/examples')  #=> '/examples'
url_for(@example)  #=> '/examples/:id'

# parameters are not kept if destination controller is different from current controller
url_for(controller: 'examples2', action: :index)  #=> '/examples2'

# parameters are not kept if you put "keep_params: false" into args
url_for(action: :show, keep_params: false)  #=> '/examples/:id'

Parameters are kept for form with GET method via hidden fields. This feature is supported by form_with for rails >= 5.1.

<%= form_with url: { action: :index}, method: :get do %>
  <%= submit_tag 'submit' %>
<% end %>
#=> ...<input type="hidden" name="key1" value="**" />
#      <input type="hidden" name="key2" value="**" /></form>

Options

Argument type

Enable only specific argument type of url_for:

# hash arg (same as default behaviour)
keep_params :key1, :key2, for: :hash
url_for(action: :index)  #=> '/examples?key1=**&key2=**'

# string arg
keep_params :key1, :key2, for: :string
url_for('/examples')  #=> '/examples?key1=**&key2=**'

# model arg
keep_params :key1, :key2, for: :model
url_for(@example)  #=> '/examples/:id?key1=**&key2=**'

:for allows to set multiple argument type as follows:

keep_params :key1, :key2, for: [:hash, :model]

Multiple controllers

Keep parameters throught multiple controllers:

class ExamplesController < ApplicationController
  include ParamsKeeper::Controller
  keep_params :key1, :key2, to: %w(examples examples2)
end

class Examples2Controller < ApplicationController
  include ParamsKeeper::Controller
  keep_params :key1, :key2, to: %w(examples examples2)
end

Default parameters

Specify default parameters:

keep_params :key1, :key2, url_options: { fixed_param: :something }

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kanety/params_keeper_rails.

License

The gem is available as open source under the terms of the MIT License.