Module: HashAsHiddenFields

Included in:
ApplicationHelper
Defined in:
vendor/plugins/blacklight/app/helpers/hash_as_hidden_fields.rb

Overview

Rails Helper methods to take a hash and turn it to form fields, works with hash nested with other hashes and arrays, standard rails serialization style. Oddly while Hash#to_query will do this for a URL query parameters, there seems to be no built in way to do it to create hidden form fields instead.

Code taken from marklunds.com/articles/one/314

This is used to serialize a complete current query from current params to form fields used for sort and change per-page

Instance Method Summary (collapse)

Instance Method Details

- (Object) hash_as_hidden_fields(hash)

Writes out zero or more elements, completely representing a hash passed in using Rails-style request parameters for hashes nested with arrays and other hashes.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'vendor/plugins/blacklight/app/helpers/hash_as_hidden_fields.rb', line 16

def hash_as_hidden_fields(hash)
  
  hidden_fields = []
  flatten_hash(hash).each do |name, value|
    value = [value] if !value.is_a?(Array)
    value.each do |v|
      hidden_fields << hidden_field_tag(name, v.to_s, :id => nil)          
    end
  end
  
  hidden_fields.join("\n")
end