Forum: Ruby on Rails Ajax and Rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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"
    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:

<html>
<head>
  <title>Create Client</title>
<%= javascript_include_tag :defaults %>
</head>
<body>

<h1>New Client</h1>

<form action="create/<%= @client.id %>" method="POST">
<table>
  <tr>
    <td><input type="hidden" id="client_id" name="client[id]" value="<%=
@client.id %>" /></td>
    <td><input type="hidden" id="client_dateReg" name="client[dateReg]"
value="<%= @datetime %>" /></td>
  <tr>
  <tr>
    <td>First Name:</td>
    <td><input type="text" id="client_firstname"
name="client[firstname]" value="<%= @client.firstname %>" /></td>
  </tr>
  <tr>
    <td>Last Name:</td>
    <td><input type="text" id="client_lastname" name="client[lastname]"
value="<%= @client.lastname %>" /></td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input type="text" id="client_email" name="client[email]"
value="<%= @client.email %>" /></td>
  </tr>
  <tr>
    <td>Gender:</td>
    <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="<%=
remote_function(
:url => {:action => 'change_female'}) %>" /> -->


      <select id="client_gender" name="client[gender]" onChange="<%=
remote_function(
:url => {:action => 'change'}) %>">
        <option value="">- Select -</option>
        <option value="Male">Male</option>
        <option value="Female">Female</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
    <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>
    </td>
  <tr>
  <tr>
    <td>
    <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"/>
    </div>
    </td>
  <tr>

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

      <input type="text" id="console_name" name="console[name]"
value="<%= @console.name %>" /><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>
  </tr>
  <tr>
        <td colspan="2" align="center"><br/><br/><input type="submit"
id="Submit" value="CREATE" /></td>
  </tr>
</table>
</form>
<%= link_to 'Back', :action => 'list' %>
</body>
</html>


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?

Thanks
This topic is locked and can not be replied to.