Elixir/Phoenix binary_to_atom

白昼怎懂夜的黑 提交于 2020-01-20 08:55:26

问题


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

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