Β Emoji select with `gem unicode-emoji`
In this example we:
- select an emoji with a label
- save the emoji without the label to the database
HOWTO: #
- add the gems:
# Gemfile
gem "unicode-emoji" # emoji list and regex (for validations)
gem "unicode-sequence_name" # emoji names
- get the data you need;
- for example, flag-country mapping:
# app/helpers/emoji_helper.rb
module EmojiHelper
# ["π¦π¨ Ascension Island", "π¦π© Andorra"]...
EMOJI_LIST = Unicode::Emoji.list("Flags")["country-flag"].map do |c|
"#{c} #{Unicode::SequenceName.of(c).delete_prefix('FLAG:').downcase.gsub(/\b('?[^0-9])/) do
Regexp.last_match(1).capitalize
end }"
end
# [["π¦π¨ Ascension Island", "π¦π¨"], ["π¦π© Andorra", "π¦π©"]...
def emoji_for_select
EMOJI_LIST.map do |k|
[k, k.split.first]
end
end
end
- add the collection select to any column:
# app/views/posts/_form.html.erb
<%= form.select :icon, emoji_for_select, { include_blank: "Select a flag" }, {} %>
Thatβs it!
Did you like this article? Did it save you some time?