Adding form elements via a js / ajax call

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

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. [email protected] wrote:

Stuart

http://en.wikipedia.org/wiki/Dark_ambient


On 10/29/06, Dark A. [email protected] 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. [email protected] wrote:

Stuart

http://en.wikipedia.org/wiki/Dark_ambient


http://en.wikipedia.org/wiki/Dark_ambient


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
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

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

id like to do something like this but with photos.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs