2

I have installed this flag icon scss gem in my Rails 5 app.

https://github.com/eugenegarl/flag-icons-rails

I'm trying now to figure out how I can use it with my Address model so that the helper gets the country name from the Address table and uses that to display the correct flag. I can't find a way to give dynamic input to the flag helper.

The gem's readme has an instruction which I can't make sense of:

Do not add gem to assets section if you want to use flag_icon rails helper.

What are the assets? I added the gem to the gemfile, bundled and then added:

@import "flag-icon";

to my application.scss - which is an asset. I have tried removing that import line from application.scss in case the instruction in the readme means not to use that line if you want to use the helper. Removing that line didn't resolve my problem (so it's back in).

I have an address model, which has a country attribute in it.

a = Address.last.country
  Address Load (0.9ms)  SELECT  "addresses".* FROM "addresses" ORDER BY "addresses"."id" DESC LIMIT $1  [["LIMIT", 1]]
 => "AU" 

I want to be able to use "country" in the flag icon helper so that it renders the corresponding flag.

In my view, I have tried:

        <%= flag_icon(Address.last.country) %>

        <%= flag_icon(Address.last.country.to_sym) %>

Neither of these attempts work. They don't produce any errors, they just don't render a map.

The maps work, because if i try:

<%= flag_icon(:au) %>

it prints the correct flag.

How can I give the helper a dynamic country input?

Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

0

Ran into similar issue.

I use gem 'country_select'.

Turns out, the symbol was in CAPS, so wasn't working.

Try changing the following:

<%= flag_icon(Address.last.country.to_sym) %>

To:

<%= flag_icon(Address.last.country.downcase.to_sym) %>

(Notice the .downcase added above).

Brett Sanders
  • 813
  • 8
  • 21