Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've just started Rails and I'm working on a simple logging app where I have a database (Mongodb) and Rails 3.x.x

The user is going to input, via text_area, 3 short text (:what) that will be stored in the database:

include Mongoid::Document
include Mongoid::Timestamps
belongs_to :Users

field :what, :type => String
field :created, :type => Date, :default => Time.now

validates_length_of :what, minimum: 10

What is the preferred method when I want to add three of the same field into one field in the database? My failing form (just to illustrate what I want to do)

    = form_for :days, :class => 'form-horizontal' do |f|
        %table.table
            - for i in 1..3 do
                    %tr
                        %td
                            %h2= i
                        %td
                            =   f.text_area_tag 'what_[]', :size => "200x5"

The above form will not work. If I set:

  f.text_area :what 

that will only send the content of the last text area to controller. What is the best way to deal with that kind of structure? Looking forward to your beautiful answers!

share|improve this question

1 Answer

What I think you're trying to do is: have three form fields that will bet concated and saved into single db coulmn, am I right?

That's not reay good think to do because you may want to allow editing those fields later, and how you gonna split them? Better create three attributes for each of those fields and add a method on your model that will concat the fields for reading

def three_whats
  self.what1 + self.what2 + self.what3
end
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.