Last Updated: February 25, 2016
·
462
· lccro

rails helper methods to load lorempixel.com images

Simply paste the snippet bellow on your application_helper.rb (or any other _helper.rb file for that matter) and you'll get "lorem ipsum"-images to use as placeholders on your projects. I used mine on a 404 page for example.

On your views, use it like this:

<%= image_tag people_url(width: 200, height: 200), class: 'thumbnail', alt: 'People' %>

Warning Ruby 2.0 syntax ahead ;)

IMAGE_CATEGORIES = %w(abstract animals business cats city sports fashion nature food nightlife people technics transport)

 # creates helper methods for each image category:
 #
 #  abstract_url width: 200, height: 100
 #  animals_url width: 300, height: 400
 #  ...
 #
 IMAGE_CATEGORIES.each do |category|
   define_method("#{category}_url") do |width:, height:|
      "//lorempixel.com/#{width}/#{height}/#{category}/"
   end
 end