title: Adding javascript handlers to remote form object after injection with RJS
body: <p>I have a page form, where i want the user to add nested forms by clicking on a remote link ('add photo', f. ex). Upon clicking the button I inject the nested forms with RJS. The injected forms are remote forms, but after injection the forms have no javascript events attached so they just submit with http to the controller of the nested object. Beside that, everything works fine.</p>
  
  <p>My question is:</p>
  
  <p><strong>Is it possible to trigger revalidation of javascript of remote forms via RJS, after injection??</strong> </p>
  
  <p>That way my nested forms would get the proper javascript event handling and submit with AJAX.</p>
  
  <p>My page form (_form.html.erb):</p>
  
  <pre><code>&lt;% javascript 'ckeditor/ckeditor' %&gt;
  
  &lt;%= form_for @page, :html =&gt; { :multipart =&gt; true } do |f| %&gt;
    &lt;%= f.error_messages %&gt;
    &lt;p&gt;
      &lt;%= f.label :active %&gt;&lt;br /&gt;
      &lt;%= f.check_box :active %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :homepage %&gt;&lt;br /&gt;
      &lt;%= f.check_box :homepage %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :name %&gt;&lt;br /&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :description %&gt;&lt;br /&gt;
      &lt;%= f.text_area :description, :rows =&gt; 5, :cols =&gt; 80 %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :text %&gt;&lt;br /&gt;
      &lt;%= f.text_area :text, :class =&gt; 'html' %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :picture_class %&gt;&lt;br /&gt;
      &lt;%= f.select :picture_class, ['left','right','full'] %&gt;
    &lt;/p&gt;
  
   &lt;% unless @page.new_record? %&gt;
    &lt;h2&gt;Photos&lt;/h2&gt;
    &lt;%= link_to 'Add photo', add_photo_page_url(@page), :remote =&gt; true %&gt;
    &lt;div id="photos"&gt;
    &lt;%= render :partial =&gt; 'shared/forms/photo', :collection =&gt; @page.photos %&gt;
    &lt;/div&gt;
    &lt;h2&gt;Snippets&lt;/h2&gt;
    &lt;%= link_to 'Add snippet', add_snippet_page_url(@page), :remote =&gt; true %&gt;
    &lt;div id="snippets"&gt;
    &lt;%= render :partial =&gt; 'shared/forms/photo', :collection =&gt; @page.snippets %&gt;
    &lt;/div&gt;
    &lt;h2&gt;Downloads&lt;/h2&gt;
    &lt;%= link_to 'Add download', add_download_page_url(@page), :remote =&gt; true %&gt;
    &lt;div id="downloads"&gt;
    &lt;%= render :partial =&gt; 'shared/forms/download', :collection =&gt; @page.downloads %&gt;
    &lt;/div&gt;
    &lt;h2&gt;Paragraphs&lt;/h2&gt;
    &lt;%= link_to 'Add paragraph', add_paragraph_page_url(@page), :remote =&gt; true %&gt;
    &lt;div id="paragraphs"&gt;
    &lt;%= render :partial =&gt; 'shared/forms/paragraph', :collection =&gt; @page.paragraphs %&gt;
    &lt;/div&gt;
   &lt;% end %&gt;
  
    &lt;p&gt;&lt;%= f.submit %&gt;&lt;/p&gt;
  &lt;% end %&gt;
  
  &lt;%= javascript_tag 'CKEDITOR.replaceAll("html");' %&gt;
  </code></pre>
  
  <p>Controller code to build the nested photo object: (page_controller.rb):</p>
  
  <pre><code>def add_photo
    @page = Page.find(params[:id])
    @photo = @page.photos.build
  end
  </code></pre>
  
  <p>RJS code that gets executed by clicking 'add photo' (add_photo.js.rjs):</p>
  
  <pre><code>page.insert_html :bottom, :photos, :partial =&gt; 'shared/forms/photo', :object =&gt; @photo
  page.visual_effect :highlight, :photos, :duration =&gt; 2
  </code></pre>
  
  <p>The partial/form that gets injected with RJS (_photo.html.erb):</p>
  
  <pre><code>&lt;%= form_for [@page, photo], :remote =&gt; true do |f| %&gt;
  &lt;p&gt;
    &lt;%= f.check_box :active %&gt;
    &lt;%= f.text_field :name %&gt;
    &lt;%= f.file_field :photo %&gt;
    &lt;%= f.submit 'Save' %&gt;
  &lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Any help would be appreciated. I would like to solve this with nested forms (not nested attributes) and RJS, but was unable to google my way to documentation on this.</p>
  
  <p>Thanks!!</p>
  
  <p>-- Jakob</p>