Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1: Dynamic linking to images in the asset pipeline?

I have ~ 500+ flag images that I previously kept in public/images/flags/ and public/images/flags_small/. For each country in my Country model, I store the :iso_code, which is the same as the name of the flag image that corresponds to it. For example, mx.png is the name of the Mexican flag because mx is the two-letter ISO code for Mexico.

I previously had a helper method that would return the html to display the image based on the iso code of the country and whether I wanted the large or small flag.

With Rails 3.1, to comply with the asset pipeline, I'm under the impression that those images should go into the app/assets/images folder. Following from this:

  1. Can I maintain the subfolders therein?
  2. How do I use image_tag to display the appropriate images?

Edit: solution The answer below was correct, but I didn't want to type that much code each time,so I created two helper methods:

  def flag(country)
    image_tag('/assets/flags/' + country.iso_code.downcase + '.png')
  end

  def small_flag(country)
    image_tag('/assets/flag_small/' + country.iso_code.downcase + '.png')
  end
like image 706
Clay Avatar asked Feb 24 '23 07:02

Clay


1 Answers

  1. Yes, you can

  2. For example: <%= image_tag 'flags/uk.gif' %>

like image 102
Maxim Filatov Avatar answered Mar 05 '23 11:03

Maxim Filatov