Search code examples
ruby-on-railsajaxprototypejsrjs

rails 2 replace_html puts javascript into the page


In my controller I have:

  respond_to do |format|
    format.js
  end

In my rjs:

page.replace_html("source_credential_list", :partial => "source_credential_list", :object => @all_source_credentials) 

or

page.replace("source_credential_list", :partial => "source_credential_list", :object => @all_source_credentials) 

In my partial: (

<% fields_for(@brand_report_source_credential_key) do |ff|  %>
   <%= ff.select :source_credential_id , options_from_collection_for_select(@all_source_credentials,'id','credential_display_name')%>
<% end %>

In my orig view:

  <span id="source_credential_list">
    <%= render :partial => 'source_credential_list' %>
  </span>

When I do replace it works great, but only one time, the 2nd time I get a alert with a null, I suspect because the span is gone. If I do replace_html, it kinda works, it updates the span but it puts the actual rjs/javascript into the span including the new select/drop down. So something like this:

Source credential try { Element.update("source_credential_list", "\n \n\n"); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"source_credential_list\", \"\n \n\n\");'); throw e }

I tried moving the span into the partial and doing both combos, but nothing working. Im sure its something obvious....


Solution

  • I'm not sure what piece of code is triggering this action, but assuming it's triggered by a link_to_remote call, make sure you don't have an :update => "source_credential_list" parameter in your link_to_remote call -- otherwise it will both execute the RJS and update the element specified with the javascript code.

    See here: http://railsforum.com/viewtopic.php?pid=44191#p44191

    Also, when you do page.replace, it only works once because page.replace replaces the specified element, including the surrounding tags, so unless the partial you specify includes the span tag, it'll disappear the first time it's used.