Forum: Ruby on Rails RJS behave differently than render :partial?

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.
Brian N. (Guest)
on 2006-05-17 19:13
I have a method in my controller like the following (the syntax may not
be correct, I'm doing this from memory - you should be able to get the
idea though) My rhtml page has a DIV with id="country_areas", which
contains a partial that only makes a drop down of country areas for a
specific country. When changing the country in the country drop down, it
uses the :observe_field to call the below method and refresh the
country_areas partial.

def find_country_areas
  @country_areas = Country.find(:all, :condition => ("country_id =?",
params[:country_id]) )
  render :partial => "country_areas", :object => @country_areas
end

The country areas drop down is getting changed to reflect those country
areas that are for a specific country, so all is well.

Now if I change the above method to:

def find_country_areas
  @country_areas = Country.find(:all, :condition => ("country_id =?",
params[:country_id]) )
end

and use a find_country_areas.rjs:

page.relace 'country_areas', :partial => 'country_areas', :object =>
'@country_areas'

it works correctly the first time I change a country. But any subsequent
changes to the country drop down do not re-render the partial at all (ie
the same areas are populated as the first country I selected). My method
is getting called, but no refresh of the partial.

Why do these behave differently? I want to use rjs because ultimately I
have two partials that I want to refresh.

Thanks
Brian
François B. (Guest)
on 2006-05-17 19:47
(Received via mailing list)
Hi !

2006/5/17, Brian N. <removed_email_address@domain.invalid>:
> page.relace 'country_areas', :partial => 'country_areas', :object =>
> '@country_areas'

Don't pass a string as the object, but do pass the instance variable.

page.relace 'country_areas', :partial => 'country_areas',
    :object => @country_areas

Notice no apostrophes around@contry_areas.  You had an error because
your partial was receiving a String object when it expected an
Enumerable.

Bye !
Brian N. (Guest)
on 2006-05-17 20:35
François Beausoleil wrote:
> Hi !
>
> 2006/5/17, Brian N. <removed_email_address@domain.invalid>:
>> page.relace 'country_areas', :partial => 'country_areas', :object =>
>> '@country_areas'
>
> Don't pass a string as the object, but do pass the instance variable.
>
> page.relace 'country_areas', :partial => 'country_areas',
>     :object => @country_areas
>
> Notice no apostrophes around@contry_areas.  You had an error because
> your partial was receiving a String object when it expected an
> Enumerable.
>
> Bye !

I think that is the way I have it - without quotes (sorry trying to
remember something that is at home while I am at work). Otherwise, I
don't think it would even work the first time. Is there anything else
that would cause these to work differently?
Michael T. (Guest)
on 2006-05-17 21:41
(Received via mailing list)
Look at the generated code and you should see the difference.  I
imagine it's the difference between one replacing the innerHTML and
the other the outerHTML.  Note that render :partial is not the same
things as page.replace.

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