Saturday 6 June 2009

dynamic_parameters

The params hash in Rails is special. It contains not only the query/post parameters that the user asked for, but also a set of special parameters such as controller and action...

but there are times when we want to know *just* what the user has *actually* asked for (without Railsy interference, however helpful).

I was quite surprised to find that there isn't a function in Rails that actually allows me to specify these. So I wrote one.

This helper goes into application_controller.

  # helper to fetch out only the dynamic parameters of the given request (ie
  # anything in the query string or POST parameters - but without the rails
  # "special" routing parameters.
  def dynamic_parameters
    @dyn_params ||= request.request_parameters.merge(request.query_parameters).with_indifferent_access.reject {|k,v| request.path_parameters.has_key? k }.symbolize_keys
  end

No comments: