Birds-Eye.Net
All things broadband and more...
 
Web Birds-Eye.Net

What's New?

Ruby on Rails (RoR)
Programming Reference


Models
External database connections
Passing current_user into model
Passing object into model
Using static lookup tables
Validates IF
Validates MongoMapper

Views
Dynamically delete form element
Edit create nested data
HTML form field check_box
Layout jQuery datatable module
Select array
Select cascading via JS
Text_area Array
Text_area listing submit
Text field format time

Controllers
Dynamic model selection
Including first item from a sorted desc table
Using from_unixtime on epoch dates

Rack
Integrated NTLM/Kerberos Authentication
Pass-through Authentication w/ NTLM

ActionMailer
Broken links in emails

Rails General
Add, Subtract, Multiply, and Divide
Calculate number of weekdays for date range
Date->Epoch & Epoch->Date
Extract first letter of each word
Hash of hashes assignment
Using: variable as hash index

jQuery
jQuery accordion MongoDB


More to come

 

Using text_area field for Displaying Array of Text

By: Bruce Bahlmann - Contributing Author (your feedback is important to us!)

In rails development, there are times when you want to use a text_area form field to store lists of items. For example, a list of possible choices to be provided to a user to answer a particular survey question. The problem is text_area is just that, a raw text input mechanism. The following shows how to tweak the normal use of the text_area field (see example below) for the purpose of collecting survey question options.

This specific use (survey tool) is implemented using Rails and MongoDB. The basic concept is a tool for managing surveys (in the spirit of survey monkey, only much better and simpler to use). A given survey (collection) can have any number of questions (Embedded Documents) - so technically this results in a nested implementation of text_area.

Configuring this functionality requires a few minor changes to the view and controller for a given rails application. These changes will now be shown and explained starting with the view.

apps/views/survey/_form.html.erb
...
<% if value["choices"].count > 0 %>
  <% value["choice"] = '' %>
  <% value["choices"].each do |choice| %>
    <% value["choice"] = value["choice"] + choice + "\r" %>
  <% end %>
<% end %>
<tr><td>Choices:</td><td>
<%= text_area("survey[question]["+key+"]", :choices, :rows=>5, :value=>value["choice"]) %>
</td></tr>

The view side must be capable of taking the array of values provided by the controller and convert that into a useful format that the text_area field can handle. Since you can't simply display the array in the text_area (well you could but it would look like this: "testthisandthat"), the array must be read and appended (decoded) so that it will once again properly show in the text_area field. We do this by walking the array and appending the fields with a carriage return (\r) so it all looks like it did when it was submitted (albeit squeaky clean).

Since a user can create or edit (update) a question, we need to address both operations from a text_area handling perspective. Remember, text_area is just going to send us raw text (e.g. "test\rthis\rthat\rand\rthat") so we need to handle that, and covert it to an array in preparation for storage. Sometimes there is also new lines (\n) as well as carriage returns (\r) so account for that (see use of gsub below). There are also cases where there may be leading or trailing spaces, so need to account for that (see sub below).

apps/controllers/surveys_controller.rb
# Fix question choices
survey["question"].each do |key,value|
  if value["questionType"] == "1"
    ca = Array.new # default (if no choices defined)
    if value["choices"] != ""
      value["choices"] = value["choices"].gsub("\r\n", '[r]')
      value["choices"].split("[r]").each do |choice|
        choice = choice.sub(/^\s+/,'')
        choice = choice.sub(/\s+$/,'')
        ca.push(choice)
      end
    end
    survey["question"][key]["choices"] = ca
  end
end

The "create" and "update" areas of the survey controller will need to include the above routine to properly handle any survey questions submitted that include a dropdown question type with attached choices entered into a text_area field. If you are rails savvy, you will likely find a more optimized way to express these, but the above does work.

Can Birds-Eye.Net help you or your Company?
Receive your Birds-Eye.Net articles and white papers hot off the presses by adding our RSS feed to your reader.

 

 

(C) Copyright Birds-Eye.Net, All rights reserved.
It is against the law to reproduce this content or any portion of it in any form without the explicit written permission of Birds-Eye Network Services, LLC. Federal copyright law (17 USC 504) makes it illegal, punishable with fines up to $100,000 per violation plus attorney's fees.