Comma Separated Array with a Text_Field in Rails

Comma separated array with a text_field in Rails

My preference would be to create an attribute on the post.model to read in the tags. e.g.

app/models/post.rb

def tag_list
self.tags.map { |t| t.name }.join(", ")
end

def tag_list=(new_value)
tag_names = new_value.split(/,\s+/)
self.tags = tag_names.map { |name| Tag.where('name = ?', name).first or Tag.create(:name => name) }
end

Then in your view you can do:

<%= f.text_field :tag_list %>

instead of :tags

The post model will accept the tag list, split into tag names, find the tag if it exists, and create it if it doesn't. No controller logic required.

EDIT This code of course relies on your tag model having an attribute called name (if not just substitute whatever attribute you're storing the tags 'name' in), and that it's unique in the database (i.e. you're using something like validates_uniqueness_of :name in your tags model)

Rails input comma separated value from text field and store in different columns

Just use the def something=(val) function which you call all the time when you are using a = to set some variable.

class RaceTiming
# unless you dont have those fields in your database
attr_accessor :minutes, :seconds, :microseconds

def time
# returns "12:14:24" as a string
[minutes, seconds, microseconds].join(":")
end

def time=(val)
#split the val into an array ["11", "04", "123"] if it was 11:04:123
val = val.split(":")
self.minutes = val[0]
self.seconds = val[1]
self.microseconds = val[2]
end
end

you call it by

record = RaceTiming.new
record.time = "12:44:953"
# do what you need to do

store text_field in rails separate by comma in many to many assosiation

The reason you are getting this is because the field assignment goes into a infinite loop with self association. You can use an attr_accessor to get the user entered information into a Serialized (Array) column.

It would look something like this:

PaymentSupplier

class PaymentSupplier < ActiveRecord::Base
...

serialize :folio_ids, Array
attr_accessor :folio_ids_text

def folio_ids_text=(ids)
self.folio_ids = ids.split(",").map(&:strip) # added map(&:strip)
end
end

form

<%= f.text_field :folio_ids_text, :class => 'form-control' %>

Make sure you account for folio_ids_text in strong parameters. Also, it might be a good idea to dynamically associate objects/relations, instead of having to manually enter IDs :).

Converting comma separated string into array for postgres in rails gives unexpected results

Got it working with

tags = category_params[:tags].split(',')
Category.create(category_params.merge({tags:tags}))

Create comma separated array using each loop

How about this? You can use Array#push.

ip_list = [];
hostnames.each do |host|
ip_list.push host.ip
end
p ip_list

Or, Array#map would be more useful.

ip_list = hostnames.map { |host| host.ip }
p ip_list

How do I read in a text file (containing comma separated numbers) into a list of integers in Ruby?

Read the file, get rid of newline, split on commas and translate elements into integers:

File.read("a.txt").strip.split(',').map(&:to_i)


Related Topics



Leave a reply



Submit