# Element belongs to Form

**URL:** <https://www.ruby-forum.com/t/element-belongs-to-form/148979>\
**Category:** Rails\
**Created:** [September 18, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/element-belongs-to-form/148979 "2008-09-18T21:26:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Commander\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Commander\_J](https://www.ruby-forum.com/u/Commander_J)\
**Post date:** [September 18, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/element-belongs-to-form/148979/1 "2008-09-18T21:26:49Z")

</div>

Hi,  
You are welcome to comment on every aspect of this post – not just the  
core  
question at the bottom.

My application renders forms previously stored in the database. A Form  
consists of Elements:

```
  t.references :form,
    :comment => 'What form we belong to.'
  t.decimal :ordering,
    :comment => 'In what order we should appear.'
  t.string :name,
    :null => false,
    :comment => 'Name.'
  t.string :title,
    :comment => 'Title.'
  t.string :ml_type,
    :null => false,
    :comment => 'Markup language type.'
  t.text :value,
    :null => false,
    :comment => 'Value.'
  t.text :flags, :comment => 'Flags.'

```

ml\_type could be ‘text’, ‘select’ or any other HTML form type.

Models:

class Element \< ActiveRecord::Base

# Relationships

belongs\_to :form

# Validations

validates\_numericality\_of :ordering  
validates\_presence\_of :name  
end

class Form \< ActiveRecord::Base  
has\_many :elements  
end

An Element should never be orphaned (i.e. without a Form). So if you  
want to  
add an Element, you go to

[http://localhost:3000/forms/1](http://localhost:3000/forms/1)

… which shows a Form’s name with its Elements.

At the bottom of the Forms#show page I have a link ‘New Element’ which  
links  
to:

[http://localhost:3000/elements/new?form\_id=1](http://localhost:3000/elements/new?form_id=1)

Problem here is that if you click Create and validation doesn’t pass, it  
forgets about the form\_id parameter.  
How can I make it remember the form\_id?

Best regards,  
CmdJohn

---

<div class="post-metadata">

**Author:** ![Commander\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Commander\_J](https://www.ruby-forum.com/u/Commander_J)\
**Post date:** [September 18, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/element-belongs-to-form/148979/2 "2008-09-18T22:18:14Z")

</div>

You can store the form\_id in a hidden field (hidden\_field\_tag).

That value will come back in the params hash, to be retrieved and  
re-used in your else case when validation fails in the create method for  
element.

---

<div class="post-metadata">

**Author:** ![Commander\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Commander\_J](https://www.ruby-forum.com/u/Commander_J)\
**Post date:** [September 19, 2008, 7:38pm UTC](https://www.ruby-forum.com/t/element-belongs-to-form/148979/3 "2008-09-19T19:38:59Z")

</div>

Thanks, that worked.

What’s the difference between hidden\_field and hidden\_field\_tag?

On Thu, Sep 18, 2008 at 10:18 PM, Ar Chron  
\<removed\_email\_address@domain.invalid
