In this example we:

  • select an emoji with a label
  • save the emoji without the label to the database

flag emoji select

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!