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

  • SEARCH
  • Home
  • 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 6861227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:30:29+00:00 2026-05-27T02:30:29+00:00

I need to create a form in my rails3 application that, when saved, will

  • 0

I need to create a form in my rails3 application that, when saved, will validate the fields and then submit them. I need a row for each field essentially.

The reason for this is that each ‘user’ has multiple attributes. For example:

  • User-Password
  • Expiration
  • Access-Group

Have tried a nested form but that doesn’t really work for me.

In my console, this works pretty well for me:

user = Array.new
user << {:username => "jenny", :attribute_name => "User-Password", :value => "123"}
user << {:username => "jenny", :attribute_name => "Expiration", :value => "123"}
user << {:username => "jenny", :attribute_name => "Access-Group", :value => "123"}
User.create(user)

That inserts a number of rows, each with the same username but different attribute names and values. Perfect.

My problem is, how do I do this using a single form?? Initially I had a parent model and a nested form but I can’t figure it out.

  • 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-05-27T02:30:30+00:00Added an answer on May 27, 2026 at 2:30 am

    This should actually be pretty straightforward nested form. Let us assume

    class User
      has_many :attributes
      accepts_nested_attributes_for :attributes, :reject_if => :all_blank, :allow_destroy => true
    end
    
    class Attribute
      belongs_to :user
    end
    

    and an Attribute has an attribute_name, a value and a user_id.

    Then your form, using haml, simple_form and cocoon would look like

    = simple_form_for @user do
      = f.input :name
      #attributes
        = f.simple_fields_for :attributes do |attribute|
          = render 'attribute_fields', :f => attribute
        .links
          = link_to_add_association 'add new attribute', f, :attributes
    

    and you add a partial called _attribute_fields.html.haml

    .nested-fields
      = f.input :attribute_name
      = f.input :value
      = link_to_remove_association "remove attribute", f
    

    If the attributes are fixed, you could easily change the input for attribute_name to

    = f.input :attribute_name, :as => :select, :collection => {'User-Password', 'Expiration', 'Access-Group' }
    

    If you want to read more about different types of nested forms, I have written a blogpost about it in greater detail.

    [EDIT] Adding server-side validation to Attribute model:

    Inside class Attribute you need to add:

    validate :check_valid_values
    
    def check_valid_values
      if attribute_name == 'Expiration'
        errors.add(:value, "Must be a valid date for Expiration") unless value.is_a_valid_date?
      end
    end
    

    Note that the method is_a_valid_date? does not exist, this is just to provide a small example. In this validation method you would then add all the possible attribute-value combinations with their validation.

    Hope this helps.

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

Sidebar

Related Questions

I need to create a form that has many of the same fields, that
I'm trying to create a form using textarea and a submit button that will
I cannot figure out how I can setup a form that will create a
I need to create a form with, half linear view (textboxes and dropdownlists in
I need to create a full-text search form for a database of emails /
I need to create a Gold bar for my windows form similar to what
Need to create a custom DNS name server using C which will check against
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I'm trying to create a multi-level ajax form on my rails app that can
I need to add a select box next to each field in a create/update

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.