Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:29:36+00:00 2026-06-09T00:29:36+00:00

In my Rails 3.0 app, I’d like to add a form element where a

  • 0

In my Rails 3.0 app, I’d like to add a form element where a user can create a series of up to 5 keyword/value pairs, to be stored in a single field. The keywords are defined by the user, not the app, so the other SO questions I’ve researched wont work because in those examples the code has to ‘know’ the keys ahead of time.

The key will always be a single word, the value will always be a single short sentence.

So 1 user might want to somehow enter the equivalent of:
{‘snow’ => ‘I like snow’,
‘ski’ => ‘I like to ski’}

Where another user might enter the equivalent of:
{‘red’ => ‘My favorite color is red’,
‘yellow’ => ‘Dont eat yellow snow’}

I get how to use the serialize :options, Hash for my model to the database stores/retreives the hash in a single a text field.

What I don’t get is an easy way to allow the user to create/edit the hash without having to actually type it in json or hash format in a text_area field.

BRUTE FORCE:
Since I have a fixed & small number of permitted pairs (5), in my view I could provide 10 fields for them to enter 0-5 pairs, then have the form save and show logic somehow move the pairs from/to those 10 fields. But I cannot figure out the logic to map those 10 fields to/from the serialized data. Any help would be appreciated.

ELEGANT:
Even better of course would be a way to dynamically generate the hash-edit form elements. (I think I read Rails 3.2 has that capability but upgrading right now isn’t possible as this is a large, complex app with a lot of moving parts (integration with multiple externals services) so we won’t be updating to a newer rails for 4-6 months.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:29:39+00:00Added an answer on June 9, 2026 at 12:29 am

    Not a recipe, just few hints.

    Brute force:

    Create 10 virtual attributes for your model (key1, value1, key2, value2, ...)

    Use ActiveRecord callbacks to serialize / deserialize the hash

    class YourModel < ActiveRecord::Base
      attr_accessor :key1, :value1, :key2, :value2, ...
      attr_accessible :key1, :value1, :key2, :value2, ...
    
      after_initialize do |obj|
        #map pairs to virtual attributes
        unless obj.pairs.nil?
          i=1
          pairs.each_pair do |key, val|
            obj.instance_variable_set("@key#{i}", key)
            obj.instance_variable_set("@value#{i}", val)
            i += 1
          end
        end
      end
    
      before_save do |obj|
        #serialize top hash
      end
    end
    

    Second way:

    Create custom form builder that generates required fields ( here’s example for radio buttons – Example of a rails custom formbuilder for a radiobutton list , you can easily find several screencasts and tutorials about form builders)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple rails app: I have 2 models, user and intro [which is simply a
My rails app talks to a couple different APIs, and I'd like to write
For a Rails app, I would like to overload or modify Kernel::load such that
Our Rails app is using Restful Authentication for user/session management and it seems that
My Rails app is hosted on Heroku. I would like to have another server
My rails app has a user model that has a HABTM relationship with interests
In my rails app, i have a create button as such def create @client
In my ruby-on-rails app, I have nested comments that can be nested an arbitrary
On my rails app I have a list of items (like a task list)
In my Rails app, when creating a business I have a form that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.