Best way to alter user input in rails

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

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 =

      redirect_to @ipsum


    def ipsum_params
      params.require(:ipsum).permit(:title, :author, :words)

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'

Source: ruby

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.