Forum: Ruby on Rails Ajax and Rails

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Matthew L. (Guest)
on 2007-06-11 21:18
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"
      @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"


  def new
    @client =
    @console =
    @datetime =

  def create
    @client =[:client])
    @console =[:console])
    @client.consoles << @console

      flash[:notice] = 'Client saved succesfully!'
      redirect_to :action=> 'index'
      render :action => 'new'

    def change


    def addConsole


    def delConsole


And here is my .rhtml:

  <title>Create Client</title>
<%= javascript_include_tag :defaults %>

<h1>New Client</h1>

<form action="create/<%= %>" method="POST">
    <td><input type="hidden" id="client_id" name="client[id]" value="<%= %>" /></td>
    <td><input type="hidden" id="client_dateReg" name="client[dateReg]"
value="<%= @datetime %>" /></td>
    <td>First Name:</td>
    <td><input type="text" id="client_firstname"
name="client[firstname]" value="<%= @client.firstname %>" /></td>
    <td>Last Name:</td>
    <td><input type="text" id="client_lastname" name="client[lastname]"
value="<%= @client.lastname %>" /></td>
    <td><input type="text" id="client_email" name="client[email]"
value="<%= %>" /></td>
    <!-- Male&nbsp;<input type="radio" id="male_gender"
name="client[gender]" value="Male" onClick="<%= remote_function(
:url => {:action => 'change'}) %>" />Female&nbsp;<input type="radio"
id="female_gender" name="client[gender]" value="Female" onClick="<%=
:url => {:action => 'change_female'}) %>" /> -->

      <select id="client_gender" name="client[gender]" onChange="<%=
:url => {:action => 'change'}) %>">
        <option value="">- Select -</option>
        <option value="Male">Male</option>
        <option value="Female">Female</option>
    <div id="male_form" style="display: none">
      Have you been tested for prostate cancer?&nbsp;&nbsp;Yes <input
type="radio" id="client_isTested" name="client[isTested]" value="1"
/>&nbsp;No <input type="radio" id="client_isTested"
name="client[isTested]" value="0" />
    <div id="female_form" style="display: none">
      Have you been tested for breast cancer?&nbsp;&nbsp;Yes <input
type="radio" id="client_isTested" name="client[isTested]"
value="1"/>&nbsp;No <input type="radio" id="client_isTested"
name="client[isTested]" value="0"/>

    <td valign="top">Console:</td>

      <input type="text" id="console_name" name="console[name]"
value="<%= %>" /><br/>
      <button type="button" id="AddConsole" onClick="<%=
remote_function( :url => {:action => 'addConsole'}) %>">+</button>&nbsp;
      <button type="button" id="DelConsole" onClick="<%=
remote_function( :url => {:action => 'delConsole'}) %>">-</button>

        <td colspan="2" align="center"><br/><br/><input type="submit"
id="Submit" value="CREATE" /></td>
<%= link_to 'Back', :action => 'list' %>

And here is my .rjs:

page.insert_html :before, 'console_name', "<input type='text'
id='console_name' name='console[name]'/> <br/>"

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?

This topic is locked and can not be replied to.