Re: Multiple child models in a dynamic form

Hi Marsvin,
You are right, just i modified the collection_select to what you suggested like:
<td><%= ts_form.collection_select :videoCodec_id, @videocodecs, :id, :name %></td><td></td>

Now am able to upload multiple times in single form.

Now i want to display all the parameters i uploaded in show page.Presently the show page displaying only the parameters uploaded in the last.
Ex:
im uploading stream details two times so i want to display both the stream details in show page instead of displaying last uploaded stream details.
To implement this i think i need to keep a for loop in show page.
Can you suggest me how to achieve this ?
my show page;
------------
<div id="content">
<table>
<table border="1">
<tr id="content-odd">
<tr  style="background-color: #CCFFCC;">
            <th width="200" align="left">Stream Name </th>
            <td><%=h @containerformat.streamName %>  </td>
            </tr>

<tr id="content-even">
<tr  style="background-color: #CCFFCC;">
            <th width="200" align="left">Location</th>
            <td> <%=h @containerformat.location %>    </td>
            </tr>


<tr id="content-odd">
<tr  style="background-color: #CCFFCC;">
            <th width="200" align="left">Containerfmt</th>
            <td> <%=h @containerformat.containerFmt %>    </td>
            </tr>

<tr id="content-even">
<tr  style="background-color: #CCFFCC;">
            <th width="200" align="left">Num programs</th>
            <td> <%=h @transportstream.numPrograms %>    </td>
            </tr>
           
<tr id="content-odd">
<tr  style="background-color: #CCFFCC;">
            <th width="200" align="left">Encryptionstd</th>
            <td> <%=h @transportstream.encryptionstd %>    </td>
            </tr>
           
           
<tr id="content-even">
<tr  style="background-color: #CCFFCC;">
            <th width="200" align="left">Pid</th>
            <td> <%=h @program.pid %>    </td>
            </tr>
           
<tr id="content-odd">
<tr  style="background-color: #CCFFCC;">
    <th width="200" align="left">Closed Caption</th>
    <td> <%=h @program.closedcaption %>    </td>
    </tr>

<tr id="content-even">
<tr  style="background-color: #CCFFCC;">
    <th width="200" align="left">Private Data pid</th>
    <td> <%=h @program.privatedatapid %>    </td>
    </tr>

           
<tr id="content-odd">
<tr  style="background-color: #CCFFCC;">
            <th width="200" align="left">videoCodec</th>
            <td> <%=h @stream.videocodec.name %>    </td>
            </tr>
           
<tr id="content-even">
<tr  style="background-color: #CCFFCC;">
            <th width="200" align="left">audioCodec</th>
            <td> <%=h @stream.audiocodec.name %>    </td>
            </tr>
           
<tr id="content-odd">
<tr  style="background-color: #CCFFCC;">
            <th width="200" align="left">resolution</th>
            <td> <%=h @stream.resolution %>    </td>
            </tr>
           
<tr id="content-odd">
<tr  style="background-color: #CCFFCC;">

<th width="200" align="left">audioSamplingRate</th>
<td> <%=h @stream.audioSamplingRate %>    </td>
</tr>

<tr id="content-odd">
<tr  style="background-color: #CCFFCC;">
    <th width="200" align="left">AspectRatio</th>
    <td> <%=h @stream.aspectRatio %>    </td>
</tr>
<tr id="content-odd">
<tr  style="background-color: #CCFFCC;">
    <th width="200" align="left">NoOfChannels</th>
    <td> <%=h @stream.noChannel %>    </td>
</tr>
<tr id="content-odd">
<tr  style="background-color: #CCFFCC;">
    <th width="200" align="left">audioSamplingRate</th>
    <td> <%=h @stream.audioSamplingRate %>    </td>   
<tr id="content-even">
<tr  style="background-color: #CCFFCC;">
            <th align="left">Tags</th>
            <td><%= @containerformat.tags_as_text %></td>
            </tr>
   </tr>
</table>

<td><%= link_to "download", {:action => "download", :id => @containerformat.id} %></td> |
<%= link_to 'Edit', edit_containerformat_path(@containerformat) %> |
<%= link_to 'Back', redirect_back()%>
</div>
----
from the above code i am just printing the uploaded data.

But as i am uploading steram details two times i want to display the stream parameters like:resolution,samplingrate,aspectratio,videocodec,audiocodec of both the sterams.

Can you suggest me how to implement the for loop in show page.
And thanks for your Help.

regards
Srikanth

Re: Multiple child models in a dynamic form

If I understood your application correctly your Stream belong_to a Program right? If that's the case you should be able to loop through them using @program.streams:

<% @program.streams.each do |stream| -%>
  <tr>
    <td>Video codec</td>
    <td><%= h stream.videocodec.name %></td>
  </tr>
  <tr>
    <td>Audio codec</td>
    <td><%= h stream.audiocodec.name %></td>
  </tr>
  # etc ...
<% end -%>

Last edited by marsvin (2009-04-13 17:50:16)

Re: Multiple child models in a dynamic form

Hi marsvin,
You understood my application correctly.
And currently my stream belongs_to program.

And i tried the same what you suggested above ,with that code i am getting errors like:
NoMethodError in Containerformats#show
Showing app/views/containerformats/show.html.erb where line #61 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name

Extracted source (around line #61):

58:  <% @program.streams.each do |stream| -%> 
59:  <tr>   
60:  <td>Video codec</td>   
61:  <td><%= h stream.videocodec.name %></td>
62:  </tr>
63:  <tr>
64:  <td>Audio codec</td> 
 

so just i tried to change code to:
---------------------------------
<% @program.streams.each do |stream| -%> 
<tr>   
<td>Video codec</td>   
<td><%= h @stream.videocodec.name %></td>
</tr>
<tr>
<td>Audio codec</td> 
<td><%= h @stream.audiocodec.name %></td>
</tr> 
<% end -%>
-------------------------
Here i am not getting any error.But the loop printing the same details .
my show page contents are:
---------------------------
Stream Name  image001.jpg 
Location //172.24.191.68/data/image001.jpg 
Containerfmt ts 
Num programs 1 
Encryptionstd no 
Pid 1234 
Closed Caption no 
Private Data pid 4321 
Video codec Mpeg4
Audio codec WMA
Video codec Mpeg4
Audio codec WMA
----------------------
from the above result audio and videocodec names are same,It should be different.
ex:i uploaded stream with two audio and video different parameters like H264,AAC and MPEG4,MP3 the show page shoud display like:
Video codec H264
Audio codec AAC
Video codec Mpeg4
Audio codec MP3

but presently the show page displaying the details of last uploaded sream that many times.

In 'containerformat controller' my 'show' method defined like:
------------------------------------------
def show
    @containerformat = Containerformat.find(params[:id])
    @transportstream = Transportstream.find_by_containerformat_id(@containerformat.id)
    @program = Program.find_by_transportstream_id(@transportstream.id)
    @stream = Stream.find_by_program_id(@program.id)
    @stream.videocodec = Videocodec.find(@stream.videoCodec_id)
    @stream.audiocodec = Audiocodec.find(@stream.audioCodec_id)

      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @containerformat }
      end
  end
----------------------
stream model:
-------------
class Stream < ActiveRecord::Base
    belongs_to :containertype
    belongs_to :videocodec
    belongs_to :imagecodec
    belongs_to :speechcodec
    belongs_to :audiocodec
    belongs_to :program
    belongs_to :containerformat
    belongs_to :transportstream
end
------------------------------------
And the current show page is from the views of containerformats where i am im trying to disply the attributes of different models.

Can you suggest me where i am doing the mistake?
And help me out to resolve this problem?

Thanks for your help.

THANKS
Srikanth

Re: Multiple child models in a dynamic form

The code is a little complicated so I missed this before but I think the problem is in setting the codec ids in the stream objects.

This error:

srikanth wrote:

NoMethodError in Containerformats#show
Showing app/views/containerformats/show.html.erb where line #61 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name

Means that @program.streams does exist but stream.videocodec doesn't (is nil). So it seems that the association between stream and video/audio codec isn't being saved, at least not for all streams.

It's my guess that either the form isn't submitting the ids properly or the program model isn't processing the stream_attributes= properly.

Could you post the parameters from the log when you submit the form? That's the most likely place things are going wrong I think.

Re: Multiple child models in a dynamic form

I just implemented your solution with the javascript buttons and it works as advertised. It enabled me to knock 30 LOC off my model smile

There is just a little update required if you use attr_accessible on your model:

attr_accessible :tasks_attributes

EDIT: I have to say that the method works perfectly with Paperclipped attachments smile

A strange little behavior: I noticed that there are numerous SELECT issued, I don't know if it is related to validation that goes crazy. I'll investigate on that later.

Last edited by Johnson (2009-04-14 12:12:56)

Re: Multiple child models in a dynamic form

Hi Marsvin,
Thank you very much for your support giving to me .

I have checked in log file.What i understood is the parameters of both the streams with audio and video codec ids are saving.But i dont know why the problem is coming.

Below are my log details :
--------------------------
Processing ContainerformatsController#new (for 127.0.0.1 at 2009-04-15 09:56:46) [GET]
  SQL (0.0ms)   SET NAMES 'utf8'
  SQL (0.0ms)   SET SQL_AUTO_IS_NULL=0
  Containerformat Columns (0.0ms)   SHOW FIELDS FROM `containerformats`
  Transportstream Columns (16.0ms)   SHOW FIELDS FROM `transportstreams`
  Program Columns (0.0ms)   SHOW FIELDS FROM `programs`
  Stream Columns (0.0ms)   SHOW FIELDS FROM `streams`
  Containertype Load (0.0ms)   SELECT * FROM `containertypes` 
  Videocodec Load (0.0ms)   SELECT * FROM `videocodecs` 
  Audiocodec Load (0.0ms)   SELECT * FROM `audiocodecs` 
Rendering template within layouts/standard
Rendering containerformats/new
  Videocodec Columns (0.0ms)   SHOW FIELDS FROM `videocodecs`
  Audiocodec Columns (16.0ms)   SHOW FIELDS FROM `audiocodecs`
Rendered containerformats/_stream (0.0ms)
Completed in 204ms (View: 47, DB: 32) | 200 OK [http://localhost/containerformats/new]


Processing ContainerformatsController#create (for 127.0.0.1 at 2009-04-15 09:58:30) [POST]
  Parameters: {"commit"=>"Create", "stream"=>{"audioSamplingRate"=>"44", "aspectRatio"=>"4:3", "resolution"=>"176x144", "noChannel"=>"stereo", "audioCodec_id"=>"1", "videoCodec_id"=>"1"}, "authenticity_token"=>"d00a688dccbbaaa1f60371e869ab8227bf9374b7", "containerformat"=>{"streamName"=>"", "containerFmt"=>"ts", "location"=>#<File:C:/DOCUME~1/x0088699/LOCALS~1/Temp/CGI.5504.2>}, "program"=>{"pid"=>"1223", "privatedatapid"=>"3425", "numvideo"=>"2", "stream_attributes"=>[{"audioSamplingRate"=>"48", "aspectRatio"=>"16:9", "resolution"=>"1280x720", "noChannel"=>"dual", "audioCodec_id"=>"3", "videoCodec_id"=>"3"}], "transportstream_id"=>"", "closedcaption"=>"yes", "numaudio"=>"2"}, "transportstream"=>{"encryptionstd"=>"no", "containerformat_id"=>"", "numPrograms"=>"1"}}
  SQL (0.0ms)   SET NAMES 'utf8'
  SQL (0.0ms)   SET SQL_AUTO_IS_NULL=0
  Containerformat Columns (0.0ms)   SHOW FIELDS FROM `containerformats`
  Transportstream Columns (0.0ms)   SHOW FIELDS FROM `transportstreams`
  Program Columns (0.0ms)   SHOW FIELDS FROM `programs`
  Stream Columns (0.0ms)   SHOW FIELDS FROM `streams`
  SQL (0.0ms)   BEGIN
  Containerformat Create (0.0ms)   INSERT INTO `containerformats` (`streamName`, `containerFmt`, `updated_at`, `containerformat_id`, `location`, `created_at`) VALUES('Assignment2.jpg', 'ts', '2009-04-15 04:28:30', NULL, '//172.24.191.68/data/Assignment2.jpg', '2009-04-15 04:28:30')
  Transportstream Create (0.0ms)   INSERT INTO `transportstreams` (`updated_at`, `encryptionstd`, `containerformat_id`, `transportstream_id`, `numPrograms`, `created_at`) VALUES('2009-04-15 04:28:30', 'no', 16, NULL, 1, '2009-04-15 04:28:30')
  Program Create (0.0ms)   INSERT INTO `programs` (`pid`, `program_id`, `privatedatapid`, `updated_at`, `numvideo`, `transportstream_id`, `closedcaption`, `numaudio`, `created_at`) VALUES(1223, NULL, 3425, '2009-04-15 04:28:30', 2, 18, 'yes', 2, '2009-04-15 04:28:30')
  Stream Create (0.0ms)   INSERT INTO `streams` (`name`, `microMedia`, `pixelPackaging`, `audioSamplingRate`, `speechDuration`, `speechBitRate`, `program_id`, `updated_at`, `resolution`, `aspectRatio`, `audioBitrate`, `imageChromaFormat`, `programaudio_id`, `containertype_id`, `frameRate`, `chromaformat`, `videoLevel`, `noChannel`, `speechSamplingRate`, `videoProfile`, `audioDuration`, `videoDuration`, `audioCodec_id`, `speechNoChannel`, `programstream_id`, `speechAULaw`, `interlaced`, `closedCaptionTeleText`, `analogProtectionSystem`, `speechCodec_id`, `containerformat_id`, `transportstream_id`, `subtitle_id`, `noFrames`, `videoBitrate`, `ipbFrameCount`, `videoFormat`, `rateControl`, `noProgram`, `encryption`, `standard`, `audioLevel`, `imageFormat`, `imageResolution`, `gop`, `audioProfile`, `contenttype`, `source`, `videoCodec_id`, `imageCodec_id`, `created_at`, `location`, `programDetail`) VALUES(NULL, NULL, NULL, '48', NULL, NULL, 18, '2009-04-15 04:28:30', '1280x720', '16:9', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'dual', NULL, NULL, NULL, NULL, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '3', NULL, '2009-04-15 04:28:30', NULL, NULL)
  Stream Create (0.0ms)   INSERT INTO `streams` (`name`, `microMedia`, `pixelPackaging`, `audioSamplingRate`, `speechDuration`, `speechBitRate`, `program_id`, `updated_at`, `resolution`, `aspectRatio`, `audioBitrate`, `imageChromaFormat`, `programaudio_id`, `containertype_id`, `frameRate`, `chromaformat`, `videoLevel`, `noChannel`, `speechSamplingRate`, `videoProfile`, `audioDuration`, `videoDuration`, `audioCodec_id`, `speechNoChannel`, `programstream_id`, `speechAULaw`, `interlaced`, `closedCaptionTeleText`, `analogProtectionSystem`, `speechCodec_id`, `containerformat_id`, `transportstream_id`, `subtitle_id`, `noFrames`, `videoBitrate`, `ipbFrameCount`, `videoFormat`, `rateControl`, `noProgram`, `encryption`, `standard`, `audioLevel`, `imageFormat`, `imageResolution`, `gop`, `audioProfile`, `contenttype`, `source`, `videoCodec_id`, `imageCodec_id`, `created_at`, `location`, `programDetail`) VALUES(NULL, NULL, NULL, '44', NULL, NULL, 18, '2009-04-15 04:28:30', '176x144', '4:3', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'stereo', NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '1', NULL, '2009-04-15 04:28:30', NULL, NULL)
  SQL (63.0ms)   COMMIT
  SQL (0.0ms)   BEGIN
  SQL (0.0ms)   COMMIT
  SQL (0.0ms)   BEGIN
  SQL (0.0ms)   COMMIT
  SQL (0.0ms)   BEGIN
  SQL (0.0ms)   COMMIT
Redirected to #<Containerformat:0x43f52b0>
Completed in 235ms (DB: 63) | 302 Found [http://localhost/containerformats]


Processing ContainerformatsController#show (for 127.0.0.1 at 2009-04-15 09:58:31) [GET]
  Parameters: {"id"=>"16"}
  SQL (0.0ms)   SET NAMES 'utf8'
  SQL (0.0ms)   SET SQL_AUTO_IS_NULL=0
  Containerformat Columns (0.0ms)   SHOW FIELDS FROM `containerformats`
  Containerformat Load (0.0ms)   SELECT * FROM `containerformats` WHERE (`containerformats`.`id` = 16) 
  Transportstream Columns (0.0ms)   SHOW FIELDS FROM `transportstreams`
  Transportstream Load (0.0ms)   SELECT * FROM `transportstreams` WHERE (`transportstreams`.`containerformat_id` = 16) LIMIT 1
  Program Columns (0.0ms)   SHOW FIELDS FROM `programs`
  Program Load (0.0ms)   SELECT * FROM `programs` WHERE (`programs`.`transportstream_id` = 16) LIMIT 1
  Stream Columns (0.0ms)   SHOW FIELDS FROM `streams`
  Stream Load (0.0ms)   SELECT * FROM `streams` WHERE (`streams`.`program_id` = 16) LIMIT 1
  Videocodec Columns (15.0ms)   SHOW FIELDS FROM `videocodecs`
  Videocodec Load (0.0ms)   SELECT * FROM `videocodecs` WHERE (`videocodecs`.`id` = 3) 
  Audiocodec Columns (0.0ms)   SHOW FIELDS FROM `audiocodecs`
  Audiocodec Load (0.0ms)   SELECT * FROM `audiocodecs` WHERE (`audiocodecs`.`id` = 3) 
Rendering template within layouts/standard
Rendering containerformats/show
  Stream Load (0.0ms)   SELECT * FROM `streams` WHERE (`streams`.program_id = 16) 


ActionView::TemplateError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name) on line #64 of app/views/containerformats/show.html.erb:
61: <tr id="content-odd">
62: <tr  style="background-color: #CCFFCC;">
63:             <th width="200" align="left">videoCodec</th>
64:             <td> <%=h stream.videocodec.name %>    </td>
65:             </tr>
66:             
67: <tr id="content-even">

    app/views/containerformats/show.html.erb:64
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:359:in `method_missing_without_paginate'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:212:in `method_missing'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:212:in `each'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:212:in `send'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:212:in `method_missing'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:359:in `method_missing_without_paginate'
    vendor/plugins/will_paginate/lib/will_paginate/finders/active_record.rb:86:in `method_missing'
    app/views/containerformats/show.html.erb:59
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/renderable.rb:39:in `send'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/renderable.rb:39:in `render'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/template.rb:73:in `render_template'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb:256:in `render'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb:367:in `_render_with_layout'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb:254:in `render'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:1174:in `render_for_file'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:905:in `render_without_benchmark'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/benchmarking.rb:51:in `render'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/core_ext/benchmark.rb:8:in `realtime'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/benchmarking.rb:51:in `render'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/mime_responds.rb:135:in `send'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/mime_responds.rb:135:in `custom'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/mime_responds.rb:160:in `call'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/mime_responds.rb:160:in `respond'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/mime_responds.rb:158:in `each'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/mime_responds.rb:158:in `respond'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/mime_responds.rb:107:in `respond_to'
    app/controllers/containerformats_controller.rb:33:in `show'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:1253:in `send'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:1253:in `perform_action_without_filters'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:617:in `call_filters'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/rescue.rb:136:in `perform_action_without_caching'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/caching/sql_cache.rb:13:in `perform_action'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/query_cache.rb:8:in `cache'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/caching/sql_cache.rb:12:in `perform_action'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:524:in `send'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:524:in `process_without_filters'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:606:in `process_without_session_management_support'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/session_management.rb:134:in `process'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:392:in `process'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:183:in `handle_request'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:110:in `dispatch_unlocked'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:123:in `dispatch'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:122:in `synchronize'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:122:in `dispatch'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:132:in `dispatch_cgi'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:39:in `dispatch'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/rails.rb:76:in `process'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/rails.rb:74:in `synchronize'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/rails.rb:74:in `process'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:159:in `process_client'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:158:in `each'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:158:in `process_client'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `run'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `initialize'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `new'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `run'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:268:in `initialize'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:268:in `new'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:268:in `run'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/configurator.rb:282:in `run'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/configurator.rb:281:in `each'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/configurator.rb:281:in `run'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/bin/mongrel_rails:128:in `run'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/command.rb:212:in `run'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/bin/mongrel_rails:281
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:142:in `load_without_new_constant_marking'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:142:in `load'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:521:in `new_constants_in'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:142:in `load'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/commands/servers/mongrel.rb:64
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:153:in `require'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:521:in `new_constants_in'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:153:in `require'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/commands/server.rb:49
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    script/server:3

Rendered F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/templates/rescues/_trace (78.0ms)
Rendered F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/templates/rescues/_request_and_response (0.0ms)
Rendering F:/srikanth-smruti/InstantRails/smruti/smruti/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/templates/rescues/layout.erb (internal_server_error)

from the above log file:
I uploaded a with two video and audio details,in which first stream videocodec audiocodec ids are:1,1
for second stream:3,3


Actually i got stuck here.

Can you suggest me how to resolve my problem.
Thanks for your help.

thanks
Srikanth

Re: Multiple child models in a dynamic form

To get a better look at what's going on I printed out your response hash as yaml and a few problems showed up.

First of all there are two places where you're trying to define the stream. First in params[:stream] and then again in params[:program][:stream_attributes]. Since you're doing @program.streams.build(params[:stream]) in your controller this is probably going to go wrong. I would remove the first "plain" stream from the form and leave only the streams in the fields_for block. That should fix at least a part of the problem.

Second I noticed the audioCodec_id in the stream_attributes segment has a typo: auioCodec_id. So audio streams won't be assigned properly.

Other than that I think it looks ok, but it's a lot of code so maybe I'm still missing something wink

This is the submission as yaml, I've highlighted the typo:

>> puts h.to_yaml
stream:                        # <==== this one should probably be removed
  audioSamplingRate: "44"
  resolution: 176x144
  aspectRatio: "4:3"
  noChannel: stereo
  audioCodec_id: "1"
  videoCodec_id: "1"
containerformat:
  streamName: ""
  containerFmt: ts
  location: ""
program:
  pid: "1223"
  privatedatapid: "3425"
  numvideo: "2"
  stream_attributes:
  - audioSamplingRate: "48"
    resolution: 1280x720
    aspectRatio: "16:9"
    noChannel: dual
    auioCodec_id: "3"          # <==== this one is misnamed
    videoCodec_id: "3"
  transportstream_id: ""
  closedcaption: "yes"
  numaudio: "2"
transportstream:
  encryptionstd: "no"
  containerformat_id: ""
  numPrograms: "1"

Re: Multiple child models in a dynamic form

By the way I also added support for sortable_element, but sometimes I get weird behavior, i.e: Rails uses the title of the first task for all the tasks, but under the hood it still uses the correct ids so changing the tasks position still works.

Re: Multiple child models in a dynamic form

Sounds good, wouldn't mind seeing how you implemented that ^^ Do you just set the position based on the order in which they were submitted?

Re: Multiple child models in a dynamic form

Hi Marsvin,

Thanks for your reply.

As you said i have gone throuth the problems.

I am doubting on the way i created the multiple models in one form.Because in secong problem as you told there is no typo problem in audioCodec_id
i have checked in log one more time.

So can you look in to my code how i am creating multiple models in one form.And suggest me if iam doing wrong.

Here i have different models like
containerformat,
transportstream,
program,stream,
videocodec,
audiocodec

Now i am creating all the models in containerformat model.

I hope you it is understadable.


my code details;
---------------
In my conatinerformat controller:

def new
       
    @containerformat = Containerformat.new
    @containerformat.transportstreams.build(params[:transportstream])
    @transportstream = Transportstream.new
        @program = Program.new
    @stream = Stream.new

    @containertypes = Containertype.find(:all)
    @videocodecs = Videocodec.find(:all)
    @audiocodecs = Audiocodec.find(:all)
   
   
    #@containerformat.transportstream.programs.build
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @containerformat }
    end
  end

-----------------
def create

    @containerformat = Containerformat.new(params[:containerformat])
    @transportstream = @containerformat.transportstreams.build(params[:transportstream])
    @program = @transportstream.programs.build(params[:program])
    @stream = @program.streams.build(params[:stream])
   
   

    respond_to do |format|
    if @containerformat.upload(params[:containerformat])
    @containerformat.location = params[:containerformat][:location].original_filename
    @containerformat.streamName = File.basename(@containerformat.location)
    @containerformat.location = '//172.24.191.68/data'
        @containerformat.location=File.join(@containerformat.location,@containerformat.streamName)
         
      if @containerformat.save and @transportstream.save  and @program.save and @stream.save
        flash[:notice] = 'Containerformat was successfully created.'
        format.html { redirect_to(@containerformat) }
        format.xml  { render :xml => @containerformat, :status => :created, :location => @containerformat }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @containerformat.errors, :status => :unprocessable_entity }
      end
    end
    end
  end
-----------------------------
def show
    @containerformat = Containerformat.find(params[:id])
    @transportstream = Transportstream.find_by_containerformat_id(@containerformat.id)
    @program = Program.find_by_transportstream_id(@transportstream.id)
    @stream = Stream.find_by_program_id(@program.id)
    @stream.videocodec = Videocodec.find(@stream.videoCodec_id)
    @stream.audiocodec = Audiocodec.find(@stream.audioCodec_id)

      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @containerformat }
      end
  end

  # GET /containerformats/new
  # GET /containerformats/new.xml
  ------------------------------------------
def edit
    @containerformat = Containerformat.find(params[:id])
    @transportstream = Transportstream.find_by_containerformat_id(@containerformat.id)
    @program = Program.find_by_transportstream_id(@transportstream.id)
    @stream = Stream.find_by_program_id(@program.id)
   
    #@stream.containertype = Containertype.find(@stream.containertype_id)
    @stream.videocodec = Videocodec.find(@stream.videoCodec_id)
    @stream.audiocodec = Audiocodec.find(@stream.audioCodec_id)
   
  end
-----------------------------------

thats all i am doing in my container format controller.

in views of containerformat controller:
--------------------------------------
new.html.erb:
-------------
div id="content">
<% form_for(@containerformat, :url=>{:action => 'create'}, :html => {:multipart => "true"}) do |f| %>
---all container formats parameters here---- 
  <%fields_for @transportstream  do |f|%>
     ---all transportstream parameters here---
<% end %>
     
  <%fields_for @program  do |f|%>
       -------- all program moedl parametes here----
     <% end %>
     
     
     <%fields_for @stream  do |ts_form|%>
          ---All stream attributes creating---
                       <% end %>

        <div id="streams">
      <%= render :partial => 'stream', :collection => @program.streams %>
    </div>
     <p><%= add_stream_link "Add a stream" %></p>
            <p>
      <%= f.submit "Create" %>
  </p>
<% end %>
--------------------------

Can you suggest me tha procedure what i have tried is correct?
If is there any other way please suggest me ?
And you help me out in deploying rails to Apache with mongrel cluster on windows.

Thanks for your help.

thanks
Srikanth

Re: Multiple child models in a dynamic form

marsvin wrote:

Sounds good, wouldn't mind seeing how you implemented that ^^ Do you just set the position based on the order in which they were submitted?

I use acts_as_list, but it's in reality acts_as_strange_list as the positions don't always get set properly. I'll try to debug it a bit.

Re: Multiple child models in a dynamic form

Great tutorial! Knocked a bunch of code off my models using this! I have the unfortunate issue of having one level deeper of a nested model. My situation is:

@release has_many :tracks
@track has_many :track_links
@track_link belongs_to :track_link_type

Think of it like an album - the user can create a new Release. Then, they can create x number of Tracks for this release. Within each track, they can also have x number of Track_Links (mp3 link, aiff link, etc). These links all have an associated Track_Link_Type. Your tutorial and others like it work perfectly until I get into the situation of adding multiple Track_Links.

Does anyone have a good way of creating unique ids which are made on insertion of the html? I have a problem where all Track_links are added to the first track only. My method of creating unique id's doesn't seem to work sad

My code - prior to refactoring using this tutorial is here:

http://railsforum.com/viewtopic.php?id=29778

Re: Multiple child models in a dynamic form

Hi guys,

I wrote something over the past week to do this: http://github.com/radar/projectr.

Enjoy.

Re: Multiple child models in a dynamic form

Hi there!

Did anyone succeed to sort or use some :order => :blabla to sort deep levels?


Thx

Re: Multiple child models in a dynamic form

Marsvin,

Thanks for the great tutorial. Worked perfectly for me.

I am curious if you could provide a sample "Show" view for the tutorial. I am wondering how to best implement a "Show" view that has the intelligence to show the child tasks whether there is 1, 10 or 100 (and maybe even 0). Thanks in advance.

Last edited by tallp (2009-04-29 00:42:34)

Re: Multiple child models in a dynamic form

Hi Marsvin,
Thank you for supporting me so far.

In my previous posts facing problem with show page.
Now i could able to get the show page for multiple streams uploaded in one form.
If you remember in my previous post you suggest that there are two problems in my code.
1.audioCodec_id is not saving.
2.multiple declarations of 'stram' model.

i tried to find the problem, finally i found the solution but it is not exact what i am looking for beacuae i hard coded the videocodecname in show page.

Below is the code i faced problem.
-----------------------------------
<% @program.streams.each do |stream| -%> 
<tr>   
<td>Video codec</td>   
<td><%= h @stream.videocodec.name %></td>
</tr>
<tr>
<td>Audio codec</td> 
<td><%= h @stream.audiocodec.name %></td>
</tr> 
<% end -%>

---------------------
in the above code
"stream.videocodec.name "
here videocodec is null so i am getting error always.

there is a association with 'stream' and 'videocodec' model.But i dont have any relation with program and videocodec model thats why in view page i am getting error.

so i tried like "program.stream.videocodec.name" not worked.

so i tried to change the code like this
-----------------------------------------
<% @program.streams.each do |stream| -%> 
<tr>   
<td>Video codec</td>   
<td><%= h @stream.videoCodec_id %></td>
</tr>
<tr>
<td>Audio codec</td> 
<td><%= h @stream.audioCodec_id%></td>
</tr> 
<% end -%>
with this code i am able to get the proper values in show page.

Now i want to implement the edit page for the streams i uploaded in form .

view page of edit:
--------------------
<%fields_for @stream  do |ts_form|%>
<table>
<tr>
<tr  style="background-color: #CCFFCC;">
    <td>Resolution</td>
    <td>Video Codec</td>
</tr>
<tr>
    <td><%= ts_form.text_field :resolution%></td>
    <td><%= ts_form.text_field :videoCodec_id %>(Enter '1' for 'H264','2' for 'Mpeg2','3' for  'Mpeg4')</td>
</tr>
</table>
<table>
<tr  style="background-color: #CCFFCC;">
    <td>audioSamplingRate</td>
    <td>Audio Codec</td>
</tr>
<tr>
    <td><%= ts_form.text_field :audioSamplingRate %></td></td>
    <td><%= ts_form.text_field :audioCodec_id %>(Enter '1' for 'AAC','2' for 'WMA','3' for  'G711')</td>
</tr>
<tr  style="background-color: #CCFFCC;">
    <td>Aspect Ratio</td>
    <td>NoOfChannels</td>
</tr>
<tr>
    <td><%= ts_form.text_field :aspectRatio %></td>
    <td><%= ts_form.text_field :noChannel %></td>
</tr>           
</table>
<%end%>

-----------------
here i am just loopig the parameters of streams which are all having the
same program_id.But As i face the  problem in show page it looping the parameters of the steram which is uploaded in last.

ex:suppose i uploaded two audio(aac,wma) and video(h264,mpeg2) stream details in on form.I am able to see all the parameters of both audio and video details in 'show page'.But if i do edit,in edit page it is displaying the stream parameters with wma and mpeg2 as audio and video codecs two times.it should show edit page for both video and audio codecs.

I hope you got my problem.

I m suspecting the problem in looping the viewe page of edit.

Can you suggest me to implement this like :
<% @program.streams.each do |stream| -%> 
---
---
---
<%end%>
i tried like above but it is not working.

thanks you.

Thanks
Srikanth

Re: Multiple child models in a dynamic form

tallp wrote:

Marsvin,

Thanks for the great tutorial. Worked perfectly for me.

I am curious if you could provide a sample "Show" view for the tutorial. I am wondering how to best implement a "Show" view that has the intelligence to show the child tasks whether there is 1, 10 or 100 (and maybe even 0). Thanks in advance.

Found this code by myself and thought I would share. It also came from Ryan's original tutorial.

<h2>Tasks</h2>
<ul>
<% for task in @project.tasks %>
  <li>
    <%=h task.name %>
  </li>
<% end %>
</ul>

Re: Multiple child models in a dynamic form

tallp wrote:

Found this code by myself and thought I would share. It also came from Ryan's original tutorial.

Ah that's a good idea. I added it to the end of the tutorial, thanks! smile

srikanth, my apologies I've been away for a few weeks. If you still need help fixing your problem please let me know!

Re: Multiple child models in a dynamic form

Hi, Thanks for the great tutorial. It really shows you how to get the most out of nested object forms.

I am attempting keeping track of the number of steps in a set of instructions (think something along the lines of ehow.com). In the form I am creating, users can add new steps to a set of instructions. Right now, I have default fields for steps 1-7. Adding one step dynamically, gets you step 8. The problem is that subsequent steps are numbered '8' also.

How do I attach an onclick event to the `link_to_function` such that clicking on the event refreshes an element on the page (using partials)?

When the user clicks the generated link, I'd like to refresh the partial containing the code so that `i` gets updated.

     def add_step_link(form_builder)
        logger.info 'ADD_STEP_LINK'
        link_to_function 'add a step' do |page|
          form_builder.fields_for :steps, Step.new, :child_index => 'NEW_RECORD' do |f|
            logger.info 'inserted js'
            html = render(:partial => 'step', :locals => { :step_form => f, :i=>@i+=1})
            page << "$('steps').insert({ bottom: '#{escape_javascript(html)}'.replace(/NEW_RECORD/g, new Date().getTime()) });"
          end
        end
      end

Re: Multiple child models in a dynamic form

thaiyoshi wrote:

How do I attach an onclick event to the `link_to_function` such that clicking on the event refreshes an element on the page (using partials)?

When the user clicks the generated link, I'd like to refresh the partial containing the code so that `i` gets updated.

Unfortunately that's not possible, at least not from within rails. Since the partial code is generated when the rails code is parsed (on the server) your @i instance won't increment when the "add_link" button is pressed, because the add_link function is javascript and excuted client side (after @i is already parsed.)

You could do this with javascript. What you'd have to do is keep track of the incrementing index in javascript and add a unique string in the place of your partial which we can replace from javascript. This should give you a basic idea:

# Put the placeholder string IDX_PLACEHOLDER in place of the index counter
html = render(:partial => 'step', :locals => { :step_form => f, :i=>"IDX_PLACEHOLDER"})
# First if we haven't defined "i" yet set it to the current number
page << "if( typeof(i) == 'undefined' ) { i = #{@i}; }"
# And increment it
page << "i++;"
# Now just insert the html as before but add an extra replace to set the index to the correct number
page << "$('steps').insert({ bottom: '#{escape_javascript(html)}'.replace(/IDX_PLACEHOLDER/g, i).replace(/NEW_RECORD/g, new Date().getTime()) });"

Now there's one more thing you have to do. The index increments when you add a new record but doesn't yet decrement when you remove one. You can change this by adding i--; to the remove link javascript.

Unfortunately this way it's still impossible to correctly number existing records if you delete one from in the middle because existing numbers won't change. If you want that as well you could write a very complicated script that counts the rows and replaces the index for each row when one is added/removed.

Come to think of it, probably a much easier option (if your layout allows it) is to use <OL> instead, letting the dom count the rows for you.

Last edited by marsvin (2009-05-11 23:05:35)