Skip to content

Instantly share code, notes, and snippets.

@narath
Last active December 14, 2015 12:09
Show Gist options
  • Save narath/5084418 to your computer and use it in GitHub Desktop.
Save narath/5084418 to your computer and use it in GitHub Desktop.
Using simple_form collections to save and load a comma separated values string as checkboxes

Based on https://gist.github.com/jchunky/3444833

add the collection helper methods to your model

class QuizAnswer < ActiveRecord::Base
  ...  
  def user_answer_collection
    return [] if !user_answer
    user_answer.split(",")
  end

  def user_answer_collection=(values)
    self.user_answer = values.join(",")
  end
end

then use this in your view

<%= f.input :user_answer_collection, :as => :check_boxes, :collection => possible_answers %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment