2

I have the following class:

class Question < ActiveRecord::Base
  serialize :choices
end

I want my questions to have a set of choices that the user can select, and I'm storing them inside the question record to avoid a second database query. I'm trying to set my form so that the text for each choice is editable by a user. This is the tag I'm using for the input:

<input name="question[choices][]" type="text" />

My permit function is this:

def question_params
  params.require(:question).permit(:category_id, :content, :choices, :answer_id)
end

My choices array isn't getting set. What am I doing wrong here?

kid_drew
  • 3,857
  • 6
  • 28
  • 38

2 Answers2

3

With the introduction of strong parameters, arbitrary non-scalar values are no longer accepted as input values. If you want to pass an array of scalars, however, you can declare that in your permit statement as in:

params.require(:question).permit(:category_id, :content, :choices => [], :answer_id)

This can be a difficult problem to detect, however, as the input values can simply be ignored without an error in some cases (the specifics of which I don't recall off-hand).

This is discussed further in how to permit an array with strong parameters

Community
  • 1
  • 1
Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
0

Try below.. (Controller code?)

class Question < ActiveRecord::Base
  serialize :choices,Array
end
beck03076
  • 3,268
  • 2
  • 27
  • 37