# Dynamic form? Not really!

**URL:** <https://www.ruby-forum.com/t/dynamic-form-not-really/85079>\
**Category:** Rails\
**Created:** [December 31, 2005, 1:12pm UTC](https://www.ruby-forum.com/t/dynamic-form-not-really/85079 "2005-12-31T13:12:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 31, 2005, 1:12pm UTC](https://www.ruby-forum.com/t/dynamic-form-not-really/85079/1 "2005-12-31T13:12:40Z")

</div>

Hi guru’s out there,

Im happilly coding my first rails app, and all goes well. I find myself  
manually coding in a particular field all the time (company\_id), that  
relates  
the contact to a company. The total coding of this form seems somewhat  
much.  
Could somebody tell me what Rails power I’m possibly missing and stuborn  
as I  
am trying to do myself?

I was wondering if the first part of the form can simply be put in the  
loop  
somehow and still be a dropdownlist. So if anybody wants to shoot,  
here’s the  
form code:

\<%= start\_form\_tag :action =\> ‘update\_contact’, :id =\> @contact %\>

### First part - My 'manual' coded stuff \<% odd\_or\_even = 0 %\> ### The cool dynamic part that shows the rest \<% odd\_or\_even = 0 for column in Contact.content\_columns odd\_or\_even = 1 - odd\_or\_even %\> \<% end %\>

| Field | Contents |
| --- | --- |
| Company | \<% @companies.each do |company| %\> \<%= company.name %\> \<% end %\> |
| \<%= column.human\_name %\>: | \<%= text\_field 'contact', column.name %\> |

\<%= end\_form\_tag %\>

Thanx a lot guys!

Regards,

## Gerard.

“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 31, 2005, 1:39pm UTC](https://www.ruby-forum.com/t/dynamic-form-not-really/85079/2 "2005-12-31T13:39:44Z")

</div>

Hi, Gerard,

On Sat, 31 Dec 2005 13:12:45 +0100  
Gerard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

 \> \<% @companies.each do |company| %\> \> \> \<%= company.name %\> \> \> \<% end %\> \> 

you can replace above with following:  
( if @companies are extracted from Company class (companies table) )

\<%= @companies = Company.find(:all)  
collection\_select(:contact, :company\_id, @companies,  
:id, :name )  
%\>

–  
Hiroshi T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 31, 2005, 2:24pm UTC](https://www.ruby-forum.com/t/dynamic-form-not-really/85079/3 "2005-12-31T14:24:49Z")

</div>

Hiroshi,

Great thanx. That worked.

But my more pressing question: Is it necessary to have the dropdownlist  
generated seperately from the full field generation loop?

Regards,

Gerard.

On Saturday 31 December 2005 13:39, Hiroshi T. tried to type  
something  
like:

> \<%= @companies = Company.find(:all)  
> collection\_select(:contact, :company\_id, @companies,  
> :id, :name )  
> %\>

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 1, 2006, 1:28pm UTC](https://www.ruby-forum.com/t/dynamic-form-not-really/85079/4 "2006-01-01T13:28:26Z")

</div>

Kevin,

I did not digest the helper chapter yet in the Agile webdev book, but I  
asume  
you mean create a shareable function called cycle, and remove the code  
from  
the view?

Grtz n Thanx

Gerard.

On Saturday 31 December 2005 14:23, Kevin O. tried to type  
something  
like:

> You can also use the ‘cycle’ helper to do the table striping.
> 
> Do something like this…
> 
> "\>
> 
> –  
> “Who cares if it doesn’t do anything? It was made with our new  
> Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”
> 
> My $Grtz =~ Gerard;  
> ~  
> :wq!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 1, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/dynamic-form-not-really/85079/5 "2006-01-01T14:16:46Z")

</div>

Gerard wrote:

> Kevin,
> 
> I did not digest the helper chapter yet in the Agile webdev book, but I  
> asume  
> you mean create a shareable function called cycle, and remove the code  
> from  
> the view?
> 
> Grtz n Thanx
> 
> Gerard.

‘cycle’ is a predefined helper for doing table striping.

See  
[http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000430](http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000430)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 31, 2005, 2:23pm UTC](https://www.ruby-forum.com/t/dynamic-form-not-really/85079/6 "2005-12-31T14:23:32Z")

</div>

You can also use the ‘cycle’ helper to do the table striping.

Do something like this…

"\>

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 1, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/dynamic-form-not-really/85079/7 "2006-01-01T16:04:51Z")

</div>

Kevin,

Thanx! That’ll be a nice cleanup in my views. Will take a look at that  
… 🙂

And best wishes to you to!

(And ofcourse to all coders the world over!)

Grtz Gerard

On Sunday 01 January 2006 14:16, Kevin O. tried to type something  
like:

> > Gerard.
> 
> ‘cycle’ is a predefined helper for doing table striping.
> 
> See  
> [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M0004)  
> 30

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!
