# Adding form elements via a js / ajax call

**URL:** <https://www.ruby-forum.com/t/adding-form-elements-via-a-js-ajax-call/74045>\
**Category:** Rails\
**Created:** [October 29, 2006, 1:28pm UTC](https://www.ruby-forum.com/t/adding-form-elements-via-a-js-ajax-call/74045 "2006-10-29T13:28:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 29, 2006, 1:28pm UTC](https://www.ruby-forum.com/t/adding-form-elements-via-a-js-ajax-call/74045/1 "2006-10-29T13:28:45Z")

</div>

I want to have a link in certain areas of my form that will say [+add]  
and that will dynamically add a form element to that area.  
For instance, if there are 2 text\_fields, and user wants 3.

I know that there are some hide/unhide options , though I’m not sure how  
that works, however I think if  
there’s a way to add them would be better .

Any suggestions ?

## Stuart

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 29, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/adding-form-elements-via-a-js-ajax-call/74045/2 "2006-10-29T14:02:47Z")

</div>

I’m running into an error that I’m unsure of how to proceed with:  
So in the form I’ve added a link\_to\_remote

\<%= link\_to\_remote “[+]”,  
:update =\> ‘xtratitle’,  
:url =\> {:action =\> “add\_title\_field”} %\>

The action:  
def add\_title\_field  
render(:partial =\> ‘txtfield’)  
end

The partial:  
\<%= text\_field(:canlocation, :city, “index” =\> 1)%\>

So all I really want to do is add a text field into the form. Maybe  
link\_to\_remote is the wrong call.  
I’m getting an error :

ActiveRecord::RecordNotFound in CanpositionsController#add\_title\_field

Couldn’t find Candidate without an ID

It looks like (this is a restful controller) that it’s hitting my  
before\_filter to find the candidate id.

Any ideas ?

TIA  
Stuart

On 10/29/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Stuart
> 
> [Dark ambient - Wikipedia](http://en.wikipedia.org/wiki/Dark_ambient)

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 29, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/adding-form-elements-via-a-js-ajax-call/74045/3 "2006-10-29T15:22:48Z")

</div>

On 10/29/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> render(:partial => 'txtfield')
> 
> ```
> 
> end
> 
> The partial:  
> \<%= text\_field(:canlocation, :city, “index” =\> 1)%\>

I got this to work, but odd that it only works once? In other words it  
won’t  
continue to generate text\_fields.  
Guess I’ll have to add another partial ?

Stuart

On 10/29/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ## Stuart
> > 
> > [Dark ambient - Wikipedia](http://en.wikipedia.org/wiki/Dark_ambient)
> 
> –  
> [Dark ambient - Wikipedia](http://en.wikipedia.org/wiki/Dark_ambient)

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 29, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/adding-form-elements-via-a-js-ajax-call/74045/4 "2006-10-29T23:03:59Z")

</div>

On Oct 29, 2006, at 7:51 AM, koloa wrote:

> cant help but if you find the answer, please let me know.
> 
> id like to do something like this but with photos.

```
Ok so you don't need an ajax call for this it is overkill. All you

```

really need is some javascript to add a new form element. So here is  
a js function that will insert a new text\_field when you click a link.

\<% form\_for(:whatever, :url =\> whateverss\_path) do |f| %\>  
\<%= text\_filed ‘whatever’, ‘foo’ %\>

\<%= link\_to\_function “[+] add”, “addNewTextField(‘buttons\_info’,  
‘canlocation’, ‘city’)” %\>

 \<%= submit\_tag "Submit" %\> 
\<% end %\>

```
Have fun with it.

```

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 29, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/adding-form-elements-via-a-js-ajax-call/74045/5 "2006-10-29T16:51:00Z")

</div>

cant help but if you find the answer, please let me know.

id like to do something like this but with photos.
