What i’ve got currently is a textarea that takes in a comma separated string of words that i want to serialize as an array in the DB.
I’ve got a model that serializes an array
class Ipsum < ActiveRecord::Base serialize :words, Array validates :title, presence: true, uniqueness: true end
Currently in my controller, I take the user input from the text area and convert the type from string to array doing the following.
class IpsumsController < ApplicationController def create params[:ipsum][:words] = params[:ipsum][:words].split(',') @ipsum = Ipsum.new(ipsum_params) if @ipsum.save redirect_to @ipsum end end private def ipsum_params params.require(:ipsum).permit(:title, :author, :words) end end
This works as expected, but i’m wondering is there’s a better way to do this, ie some kind of before_serialization callback, or some kind of form serialization.
Here’s the form currently as well, im using ruby-slim.
= form_for(@ipsum) do |f| = f.label :title = f.text_field :title = f.label :words = f.text_area :words, value: '' = f.submit 'Submit'