Showing posts with label helpers. Show all posts
Showing posts with label helpers. Show all posts

Sunday, 31 October 2010

Gravatar in rails

Just a quick code-snippet today. A quick helper to take an email and spit out a gravatar image.

  # show the gravatar image for a profile - based on their email
  def gravatar(email, size = 100)
    the_hash = Digest::MD5.hexdigest(email).to_s
    image_tag "http://www.gravatar.com/avatar/#{the_hash}?s=#{size}&d=mm"
  end

This one displays the "mystery man" image if the person doesn't have a gravatar. More details on the options and how to tweak it further are available on the Gravatar Image Requests page.

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