Diogo Wernik
Diogo Wernik

Reputation: 638

ruby on rails - how to post tagsinput and get an array as result

i created a field called tags_speciality

Here is my migration file:

class AddSpecialityToSubdomains < ActiveRecord::Migration
 def change
    add_column :subdomains, :tag_speciality, :string, array: true
 end
end

Then i added on my view file the field:

<%= f.text_field :tag_speciality, data: {role: 'tagsinput'},  multiple: true %>
<%= f.submit class: 'btn btn-primary' %>

After submit the form i get that result:

[["tag1,tag2,tag3"]]

But look like that would be better get this result:

[["tag1","tag2","tag3"]]

How can i achive that?

Thanks

Upvotes: 0

Views: 90

Answers (2)

Bartłomiej Gładys
Bartłomiej Gładys

Reputation: 4615

You can do something like this on your result:

arr = [["tag1,tag2,tag3"]]
result = arr[0][0].split(',')

#=> ["tag1", "tag2", "tag3"]

EDIT

I found better solution with flatten

arr.flatten.first
#=> ["tag1", "tag2", "tag3"]

Upvotes: 3

ahawrylak
ahawrylak

Reputation: 346

You can use Ruby's split method in your controller. For example:

"tag1,tag2,tag3".split(',')
=> ["tag1", "tag2", "tag3"]

Upvotes: 3

Related Questions