Claudiu S
Claudiu S

Reputation: 1627

Ruby String to Array method

I've been searching and can't seem to find anything. I am using Easy-Roles & CanCan in a Rails app, and my roles column is defined as an array of string. When the user inputs/ selects a role from a dropdown list, the parameters gets sent as a string rather than an array, so I can't save it in the database.

Is there any method such that I can create an 1D-1 element string array from a string?

EDIT:

Here is my form:

<%= f.collection_select :roles, User::ROLES, :to_s, :split, 
                        :prompt=>"Select a role" %>

And I get this error:

Attribute was supposed to be a Array, but was a String. -- "Admin"

Upvotes: 1

Views: 4190

Answers (2)

user1036727
user1036727

Reputation: 51

You could just use

someString.split

that would do

someString = "Hello"
someString.split
  => ["Hello"]

Upvotes: -1

Abraham P
Abraham P

Reputation: 15471

why not just

   [someString]

Where someString is a variable containing a string?

Or in your case:

  [params[:yourRoleString]]

Upvotes: 5

Related Questions