1

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

5
  • 2
    Is it the Something.new(something_params) that is complaining? Presumably :some_field is a string in something_params, right? Commented Mar 12, 2014 at 20:12
  • yes , you are right ! .. but when can i split the :some_field so that it can be stored as an array ? Commented Mar 13, 2014 at 12:58
  • I think you have to use update_attributes Commented Mar 13, 2014 at 23:58
  • What format are you expecting people to type into the <textarea>? Some sort of CSV? Commented Mar 14, 2014 at 0:04
  • 1
    a normal text separated by commas .. that's why i'm trying to split it with split function Commented Mar 14, 2014 at 13:22

2 Answers 2

1

I finally figured out what's wrong

I have to modify the hash value to be an array before calling

@something = Something.new(something_params)

so , i had to type this first :

@something[:some_field] = params[:something][:some_field].split(',')

The code that works is :

def create
    @something[:some_field] = params[:something][:some_field].split(',')
    @something = Something.new(something_params)
    if @something.save
        redirect_to @something, notice: "whatever"
    else
        render "new"
    end
end
Sign up to request clarification or add additional context in comments.

1 Comment

What version of Rails are you using?
0

Add .reject(&:blank?) after you split(","). I also use mongoid but with multi select. I changed my code to text_area and this code worked for me. Also an example http://funonrails.com/2012/01/mongoid-array-field-and-rails-form/

@something[:some_field]=params[:something][:some_field].split(',').reject(&:blank?)

2 Comments

@archie it is not assigned as an array anyway .. it is converted to a stringified array .. so , it is not about saving .. it is before saving ( i think )
You need to add reject(&:blank?) after you split array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.