Rails 5 - Flag Icons with dynamic country input

陌路散爱 提交于 2019-12-12 23:50:25

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/41133313/rails-5-flag-icons-with-dynamic-country-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!