AJAX form with multiple interaction points

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.

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