# AJAX form with multiple interaction points

**URL:** <https://www.ruby-forum.com/t/ajax-form-with-multiple-interaction-points/87649>\
**Category:** Rails\
**Created:** [March 4, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/ajax-form-with-multiple-interaction-points/87649 "2007-03-04T23:10:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![carminem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@carminem](https://www.ruby-forum.com/u/carminem)\
**Post date:** [March 4, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/ajax-form-with-multiple-interaction-points/87649/1 "2007-03-04T23:10:57Z")

</div>

Hi to all,

sorry for the poor subject but, as of now, I can’t find anything better  
to explain the problem.

What I would like to accomplish is:

1. Show the user a form asking for a badge number
2. After entering the number, a new form has to be shown to the user  
asking for other data  
2.1 This new form must give the option to add a client to the selected  
badge  
Upon adding the client to the badge, no more clients can be  
added and the  
user can just fill in the other required fields.

Problem is:  
The form at point 2, has a model bound to it (Traccia). This model has  
some validations on  
field presence.  
One of the requirements is that it must have a client attached. Thus, I  
cannot save the Traccia’s  
instance to the database until every validations is satisfied.

The action (ingresso\_uscita) which gets called has the following code:

def ingresso\_uscita  
@trace = params[:traccia].blank? ? nil : Trace.new(params[:traccia])  
if request.xhr?  
if !params[:badge].blank?  
@trace = Trace.find\_by\_badge(params[:badge])  
if @trace.nil?  
@trace = Trace.new  
@trace.inizio = Time.now  
@trace.badge = params[:badge]  
@clienti = Client.find\_by\_sql(‘select \* from clients where id  
not in (select client\_id from traces where fine is null)’)  
end  
else  
@trace.client\_id = params[:id]  
@trace.nr\_abbinati = 1  
end  
render :update do |page|  
page.visual\_effect :highlight, ‘ingresso\_uscita\_form’  
page.replace\_html ‘ingresso\_uscita\_form’, :partial =\>  
‘form\_ingressouscita’  
end  
end  
end

The first time it gets called, it shows the form described at point 1.  
When the user enters the badge number, the action gets called another  
time.  
This time, request.xhr? evaluates to true, and an instance variable  
(@trace) is created.  
When the user clicks on the link to add a client to the Trace instance  
via the following link:

\<%= link\_to\_remote(“Titolare”, :url =\> { :controller =\> “main”, :action  
=\> “ingresso\_uscita”, :id =\> cliente.id }) %\>

A silent exception is raised which complains about @trace being nil.  
I have tried passing @trace as a parameter with the above mentioned  
link, and it still failed miserably.  
The only parameter which gets passed to the action is params[:id].

Advices are welcome.

Thanks in advance for your help.
