Elixir/Phoenix binary_to_atom

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

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"}] %>


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