Ajax and Rails


#1

Hello, I am working on a form to insert multiple items in different
tables here is the code I have:

class ClientController < ApplicationController
scaffold :client

def index
if params[:letter]
cond = params[:letter] + “%”
@letter = params[:letter]
@clients = Client.find :all, :conditions => [“firstname like ?” ,
cond], :order => “firstname ASC”
else
@clients = Client.find_all
@consoles = Console.find_all
@latestclients = Client.find :all, :conditions=> “isTested = 1”,
:order => “dateReg DESC LIMIT 5”
@latestconsoles = Console.find :all, :order => “name DESC LIMIT 5”

end

end

def new
@client = Client.new
@console = Console.new
@datetime = DateTime.now
end

def create
@client = Client.new(params[:client])
@console = Console.new(params[:console])
@client.consoles << @console

if @client.save
  flash[:notice] = 'Client saved succesfully!'
  redirect_to :action=> 'index'
else
  render :action => 'new'
  end
end

def change

end

def addConsole

end

def delConsole

end

end

And here is my .rhtml:

Create Client <%= javascript_include_tag :defaults %>

New Client

First Name:
Last Name:
Email:
Gender:
  <select id="client_gender" name="client[gender]" onChange="<%=

remote_function(
:url => {:action => ‘change’}) %>">
- Select -
Male
Female

Have you been tested for prostate cancer?  Yes  No
Have you been tested for breast cancer?  Yes  No
Console:
  <input type="text" id="console_name" name="console[name]"

value="<%= @console.name %>" />


-

</td>


<%= link_to 'Back', :action => 'list' %>

And here is my .rjs:

page.insert_html :before, ‘console_name’, "
"

What I am trying to do here is that when the client clicks on the “+”
button it calls the addConsole.rjs and it adds another input field to
add one more console. Now when I create the client, it only adds the
first Console input field and not the second into my database. How can I
add multiple items of the same objects into the database?

Thanks