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
Rails 4 Strong Parameters Without Required Parameters
Sleep Until Condition Is True in Ruby
Are There Any Good Mutation Testing Tools for Ruby 1.9 and Rspec2
How to Make the Url's in Ruby on Rails Seo Friendly Knowing a @Vendor.Name
How to Detect the End of a Method Chain in Ruby
Delete Non-Utf Characters from a String in Ruby
Create Hash from Array and Frequency
Wicked-Pdf Not Showing Images, 'Wicked_Pdf_Image_Tag' Undefined
How to Get Rspec to Run All Tests Nested Under a Folder
Rails Nested With_Option :If Used in Validation
A Selenium Webdriver Exception
C1 or C2 Coverage Tool for Ruby
Ruby - Difference Between :Variable and @Variable
How to Install a Gem Globally Without Sudo Using Rbenv