I'm new in using rails with mongoid and i have problems in storing an array in some field
Here is the code to clarify everything
in my model :
class Something
include Mongoid::Document
field :some_field, type: Array
end
in my controller :
def create
@something = Something.new(something_params)
@something[:some_field] = params[:something][:some_field].split(',')
if @something.save
redirect_to @something, notice: "whatever"
else
render "new"
end
end
in my view :
<%= form_for @something do |f| %>
<div class="field">
<%= f.label :some_field %><br>
<%= f.text_area :some_field %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
when i use it that way i get an error message : "Problem: Value of type String cannot be written to a field of type Array Summary: Tried to set a value of type String to a field of type Array Resolution: Verify if the value to be set correspond to field definition"
but when i change the field type to String , it is successfully added but with a stringified array
I tried this :
render :text => @something[:some_field] #outputs : ["field1", "field2", "field3"]
but
render :text => @something[:some_field].inspect #outputs : "[\"field1\", \"field2\", \"field3\"]"
What should i do to store the value as an Array ?
Thanks
Something.new(something_params)that is complaining? Presumably:some_fieldis a string insomething_params, right?:some_fieldso that it can be stored as an array ?<textarea>? Some sort of CSV?splitfunction