问题
I have a form
<%= select f, :user_id, ["刺繡等等我": "2", "wow": "3"] %>
If I use only english language, it works perfectly. But chinese, or any other returns error
** (ArgumentError) argument error
:erlang.binary_to_atom("刺繡等等我", :utf8)
(elixir) src/elixir_parser.yrl:512: :elixir_parser.yeccpars2_93/7
I believe it has do to with the encoding. How can I convert the string to the acceptable format?
Thanks in advance!
回答1:
Atoms cannot contain codepoints above 255 as of the current version of Erlang (19).
binary_to_atom(Binary, utf8)
fails if the binary contains Unicode codepoints > 255. In a future release, such Unicode characters can be allowed and binary_to_atom(Binary, utf8) does then not fail.
Source
The ["刺繡等等我": "2"]
syntax is equivalent to [{:erlang.binary_to_atom("刺繡等等我"), "2"}]
i.e. it converts all the keys to atoms and the text you're using contains codepoints over 255.
Since select
supports any enumerable that yields 2 item tuples, you can construct the list of two element tuples of strings using the longer notation:
<%= select f, :user_id, [{"刺繡等等我", "2"}, {"wow", "3"}] %>
来源:https://stackoverflow.com/questions/41351625/elixir-phoenix-binary-to-atom