Topic: One link_to_remote for updating many <DIV>s

I have in my view the following situation:

<div id=no1>...</div>
...
<div id=no5>...</div>
...
<div id=no9>...</div>
...
<%= link_to_remote("Press me", :update => "no1", :url => {:action => "no1"}) %>

How can I update no1 and no9 with one link_to_remote statement??

Re: One link_to_remote for updating many <DIV>s

remove the :update option and instead use a RJS template:

#your view:
div id=no1>...</div>
...
<div id=no5>...</div>
...
<div id=no9>...</div>
...
<%= link_to_remote("Press me", :url => {:action => "no1"}) %>

#your RJS:
# _no1.rjs
#update the div no1 with some text
page["no1"].replace_html "Insert this Text"
#update the div no9 with a partial
page["no9"].replace_html render :partial => "somepartial"


More Info in the api docs:
ActionController::Base -> render
ActionView::Base -> Javascriptgenerator (about RJS templates)
ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods -> the page commands like replace_html etc.

Last edited by Duplex (2007-08-01 06:47:54)

Re: One link_to_remote for updating many <DIV>s

Thanks for the fast answer Duplex!!!

Isnt there a more ruby way to do it.

I saw that in PHP you can use an - array - something like:

<%= link_to_remote("Press me", [(:update => "no1", :url => {:action => "no1"}),(:update => "no9", :url => {:action => "no9"})]) %>

Why is this not possible too?
By the way, I have say I am new to Ruby so it could be my fault :-)

Re: One link_to_remote for updating many <DIV>s

No, there isn't such a way, and it wouldn't be "more Ruby/rails" in my opinion.
I don't see any advantage to such an option, but some disadvantages.

First, making multiple AJAX calls from one link would make it impossible (or at least a pain) to degrade gracefully if Javascript is disabled in the brwoser.
Second, it would complicate your code as you would end up with 2 actions that - in the end - belong to the same step in the usage process. It's loosing some readability on the controller side.

The RJS way on the other hand enables you to manipulate as many objects on the page as you wish, with one single action and one RJS template.

Re: One link_to_remote for updating many <DIV>s

It makes sence what you say, but I would say from the view of a programmer it would be easier to programm it. Unfortunately my german manual is not a help about RJS.

How do I have to integrate and call the RJS-Template _no1.rjs ???

Re: One link_to_remote for updating many <DIV>s

you could also do it in the controller...

def no1
  #some other code

  render :update do |page|
    page["no1"].replace_html "Insert this Text"
    page["no9"].replace_html render :partial => "somepartial"
  end
end

Last edited by Duplex (2007-08-01 09:31:13)

Re: One link_to_remote for updating many <DIV>s

Or if you were in a really poor-programming mood you could do a ruby/javascript hybrid.

Controller:

def no1
  #some other code

  render :update do |page|
    page << "update_nos('#{@no1}', '#{@no2}'..., '#{@no9}');"
  end
end


javascript (on page):

function update_nos(no1, no2..., no9) {
  $('no1').innerHTML = no1;
  $('no2').innerHTML = no2;
  ...
  $('no9').innerHTML = no9;
}

I'm not sure what advantages this solution would hold, but it's a way of doing it nonetheless.

P.S. I recommend going with Duplex's solution.

Kyle Peyton - Web Developer
Santa Clara, CA - Startontop

Re: One link_to_remote for updating many <DIV>s

Thanks Duplex and Weexpectedthis for your HELP.

Duplex,
your suggestion worked immediately ... Thanks very much!!! Thanks a lot!