我一直在寻找,似乎找不到任何东西。我在 Rails 应用程序中使用 Easy-Roles & CanCan,我的角色列被定义为一个字符串数组。当用户从下拉列表中输入/选择角色时,参数将作为字符串而不是数组发送,因此我无法将其保存在数据库中。
有什么方法可以从字符串创建一维-1 元素字符串数组吗?
编辑:
这是我的表格:
<%= f.collection_select :roles, User::ROLES, :to_s, :split,
:prompt=>"Select a role" %>
我得到这个错误:
Attribute was supposed to be a Array, but was a String. -- "Admin"
最佳答案
为什么不
[someString]
其中 someString 是一个包含字符串的变量?
或者在你的情况下:
[params[:yourRoleString]]
关于ruby-on-rails - Ruby 字符串转数组方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18092142/