Forum: Ruby on Rails partials and rjs

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.
E458de7b2e71b722b7204d001e222b68?d=identicon&s=25 Brian (Guest)
on 2006-06-09 14:10
I must have a misunderstanding of partials, maybe one of you can help me
out. I have a page that initially displayed via the following:

<-- in controller -->
def find_areas_for_country
    @country_areas = CountryArea.find(:all, :conditions => ["country_id
= :country", params], :order => "descr asc")
    render :partial => 'country_area'
  end

<-- my partial -->
<%= select :country_area, :id, @country_areas.collect {|c| [c.descr,
c.id]}, {:prompt => "-- Select Area --"},
  {:name => "country_area_select", :onfiltered=> "new
Ajax.Updater('country_area_info',
   '/controller/admin/change_country_area/'+country_area.selectedIndex,
   {asynchronous:true, evalScripts:true});" }
%>

<-- form containing partial -->
<form>
   <span id="country_area">
    <%= render :partial => 'country_area' %>
   </span>&nbsp;
     <span id="add_country_area" style="cursor:pointer;">New</span>
          <%= in_place_editor 'add_country_area',
       :url => {:action => 'add_country_area'},
       :with =>
"Form.serialize(form)+'&country='+document.add_trip.country.selectedIndex"
%>
 </span>
...

----------

This creates a drop down list of areas within a country. Now I have an
in_place_editor field (see above), that once you type in a new value and
press submit, calls the following method in the controller:

  def add_country_area
    @country_area = CountryArea.new(:descr => params[:value],
:country_id => params[:country])
    @country_area.save
    #find_areas_for_country
    @country_areas = CountryArea.find(:all, :conditions => ["country_id
= :country", params], :order => "descr asc")
  end

I then have the following rjs defined:
< add_country_area.rjs >
page.replace 'country_area', :partial => 'country_area', :object =>
@country_areas


----------------------

According to the docs, it looks like page.replace "Replaces the "outer
HTML" (i.e., the entire element, not just its contents) of the DOM
element with the given id"

What happens when this runs, is the origianl drop down adds the value to
drop down that was typed in - say we typed in "test", but then another
drop down along with some _javascript code is added. So it doesn't look
like it is replacing, but rather adding to it. This is what it looks
like - now I have two drop downs!

|drop down|  Element.update("country_area", |2nd drop down|

Could someone help me shed some light on what is going on and help me
figure out what I need to do to get this working such that I end up with
one drop down with whatever I typed in added to the drop down list?

Thanks a lot!
Brian
This topic is locked and can not be replied to.