1

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"
1
  • thank @Jeffrey very funny. Anyway, the reason I asked because if your attributes expect an array then perhaps your form should generate array (like select with multiple=true) for it right? If a user can only have 1 role at any given time then why use Easy-Roles at all? Commented Aug 9, 2013 at 0:11

2 Answers 2

5

why not just

   [someString]

Where someString is a variable containing a string?

Or in your case:

  [params[:yourRoleString]]
Sign up to request clarification or add additional context in comments.

1 Comment

How comes it doesn't try to interprete :yourRoleString as an index in params?
-1

You could just use

someString.split

that would do

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

1 Comment

Um, don't do this. .split is for breaking apart a string into an array: "Hi There".split #=> ["Hi", "There"]. Not only will it be confusing to anyone else who reads your code, but if you have any spaces in the string it breaks it into pieces for you, which you probably don't want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.