Updating two divs?

I have a form where I can edit details about a person. This includes
adding addresses which can be used to contact the person. A person can
have any number of addresses.

On the edit person screen I have a list of the addresses which are a
associated with that person. Clicking on ‘Add address’ causes a new form
to appear (uses link_to_remote) this form is completed and the results
are written to the database, the list of addresses on the page is
updated by re-rendering the partial.

The problem that I am having is that I’m using the :update to add the
new address to the list of addresses but I also need to remove the form
which appeared and replace it once again with the 'Add address" link.
How can I do this? Can I update 2 divs?

This is what RJS templates are for. If you google around for that you
should find what you need

Fred

I’ve tried to do this using RJS. IN my .rjs file I have:

===
page.replace_html ‘address-list’ , :partial => ‘address_list’
page.replace_html ‘address-form’ , :partial => ‘address_add’

The second part works. Replacing the complete form with the ‘Add
address’ link. However, the first part generates loads of random markup
including stuff like the following in amongst the table displaying the
addresses:

===
\n \n \n 11 A street town city \n \n \n
\n \n \n \n add \n post \n \n Delete \n Edit \n
\n \n \n 1 \n 2 \n \n Delete \n Edit \n
\n \n \n address \n postcode \n \n Delete \n Edit \n
\n \n \n fg \n fg \n \n Delete \n Edit \n
\n \n \n new \n new \n \n Delete \n Edit \n
\n \n \n here \n here \n \n Delete \n Edit \n
\n \n \n fe \n fi \n \n Delete \n Edit \n
\n \n \n dfd \n dfd \n \n Delete \n Edit \n
\n \n \n 99 \n 999 \n \n Delete \n Edit \n
\n \n

Note: the use of ‘list’ when describing how the addresses are displayed
is a little misleading. The addresses are actually displayed in a table.

What is wrong with:

class PersonController < ApplicationController
def add_address
@person = Person.find(params[:id])
@person.person_addresses.create(:address =>
Address.create(params[:address]))
end
end

and in app/views/person/add_address.rjs :

page.replace_html ‘address-table’ , :partial => ‘address_list’
page.replace_html ‘address-form’ , :partial => ‘address_add’

On 16 November 2006 12:59, Matthew Planchant wrote:

The problem that I am having is that I’m using the :update to add the
new address to the list of addresses but I also need to remove the form
which appeared and replace it once again with the 'Add address" link.
How can I do this? Can I update 2 divs?
Use RJS to do that:
class PersonController < ApplicationController
def create_address
// save address to DB

render :update do |page|
  page.replace 'addresses-list', :partial => 'address', :collection 

=>
@addresses
page.remove ‘address-form’
end
end
end

or in app/views/person/create_address.rjs :

page.replace ‘addresses-list’, :partial => ‘address’, :collection =>
@addresses
page.remove ‘address-form’

In later case, write this to controller’s action:

class PersonController < ApplicationController
def create_address
// save address to DB

respond_to do |format|
  format.js
end

end
end

dont give a :update value.

instread, render a RJS Template in your action…

def add_address
… add the address to the DB etc. …
#at the end, the action ill automatically render the RJS template
end

in your controllers views directory you would have a file called
add_address.rjs:

page.replace_html :div-to-update, :partial => “_address-list”
page.hide :ID-of-form-to-hide

see for more info:

->
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html

-> http://api.rubyonrails.org/classes/ActionView/Base.html …scroll
down to “JavascriptGenerator”

Tutorial:
http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates

Thorsten L wrote:

dont give a :update value.

instread, render a RJS Template in your action…

def add_address
… add the address to the DB etc. …
#at the end, the action ill automatically render the RJS template
end

in your controllers views directory you would have a file called
add_address.rjs:

page.replace_html :div-to-update, :partial => “_address-list”
page.hide :ID-of-form-to-hide

This is what I’ve done (see above). But I’m getting garbled mark up when
the JavaScript attempts to render a table:

===
try { Element.update(“address-table”, "\n \n Address \n Postcode \n
Preferred \n
\n \n \n 11 street town city \n postcode \n \n
\n \n \n \n 1 \n 2 \n \n Delete \n Edit \n
\n \n \n address \n postcode \n \n Delete \n Edit \n
\n \n \n street \n town \n \n Delete \n Edit \n
\n \n \n street \n town \n \n Delete \n Edit \n
\n \n \n street \n town \n \n Delete \n Edit \n
\n \n \n street \n town \n \n Delete \n Edit \n
\n \n \street \n 999 \n \n Delete \n Edit \n
\n \n \n street \n town \n \n Delete \n Edit \n
\n \n \n street \n town \n \n Delete \n Edit \n
\n \n \n street \n town \n \n

Is there a problem with rendering tables this way?

Hi

I am getting the XML from an external source via HTTPService post.
I am using CobraVsMongoose (CVM) to do a xml - hash conversion. One of
the fields in the hash is “Task_Number”, which is the id of the AR
object that I want to retrive.

Here is an example of the object that CVM returns.
{“tasks”=>{“task”=>{“Task_Number”=>{"$"=>“3”},
“Task_Name”=>{"$"=>“task_three”}, “Priority_Rank”=>{"$"=>“55”}}}}

I have this code

xml_string = params[:my_tasks]
@my_tasks = CobraVsMongoose.xml_to_hash(xml_string)


@my_tasks["tasks"]["task"].each do |hashed_task|
  @task = Task.find(hashed_task["Task_Number"]["$"].to_i)
  @task.priority = hashed_task["Priority_Rank"]["$"].to_f
  @task.save
end

which works perfectly when I have many tasks, however, when I am sending
over only one task i get this error in the development.log
TypeError (can’t convert String into Integer):

I am pretty sure that “3”.to_i should give me the integer 3. also, since
the code runs smoothly for multiple tasks in the same XML document, I am
baffled.

Thanks for the help
Ivor

On 16 November 2006 14:44, Matthew Planchant wrote:

try { Element.update(“address-form”, “Add address”); } catch (e) {
alert(‘RJS error:\n\n’ + e.toString());
alert(‘Element.update(“address-form”, “Add address”);’); throw e }
Add address

I haven’t got this by viewing the generated source. This is visible on
the page. The ‘Add address’ at the bottom is the correct link which
should be being created.

Does anyone have any idea what’s going on here?
I believe, when you use link_to_remote with :update option, it expects
HTML
back (not javascript). Try removing :update option from link_to_remote.

When I just have the following in the form (i.e. should replace the
completed for with a ‘Add address’ link):

===
page.replace_html ‘address-form’ , :partial => ‘address_add’

Then I get the following in the page:

===
try { Element.update(“address-form”, “Add address”); } catch (e) {
alert(‘RJS error:\n\n’ + e.toString());
alert(‘Element.update(“address-form”, “Add address”);’); throw e }
Add address

I haven’t got this by viewing the generated source. This is visible on
the page. The ‘Add address’ at the bottom is the correct link which
should be being created.

Does anyone have any idea what’s going on here?

Found the problem.

When there are multiple tasks, as in this case

{“tasks”=>{“task”=>[{“Task_Number”=>{"$"=>“2”},
“Task_Name”=>{"$"=>“task_two”}, “Priority_Rank”=>{"$"=>“10”}},
{“Task_Number”=>{"$"=>“8”}, “Task_Name”=>{"$"=>“task_eight”},
“Priority_Rank”=>{"$"=>“20”}}, {“Task_Number”=>{"$"=>“4”},
“Task_Name”=>{"$"=>“task_four”}, “Priority_Rank”=>{"$"=>“30”}},
{“Task_Number”=>{"$"=>“5”}, “Task_Name”=>{"$"=>“task_five”},
“Priority_Rank”=>{"$"=>“40”}}, {“Task_Number”=>{"$"=>“1”},
“Task_Name”=>{"$"=>“task_one”}, “Priority_Rank”=>{"$"=>“50”}},
{“Task_Number”=>{"$"=>“3”}, “Task_Name”=>{"$"=>“task_three”},
“Priority_Rank”=>{"$"=>“60”}}, {“Task_Number”=>{"$"=>“7”},
“Task_Name”=>{"$"=>“task_seven”}, “Priority_Rank”=>{"$"=>“70”}},
{“Task_Number”=>{"$"=>“6”}, “Task_Name”=>{"$"=>“task_six”},
“Priority_Rank”=>{"$"=>“80”}}]}}

@my_tasks[“tasks”][“task”] is an array of tasks, whereas in the case of
the single task, it gets parsed into a hash. My hack to fix this now is
to pass the same task twice if the number of tasks is only one, but this
is obviously a ugly hack.
Can anyone suggest how to change the rails code so that the
@my_tasks[“tasks”][“task”] object looks the same, whether it has 1 or
many elements?

Ivor

Maxim K. wrote:

I believe, when you use link_to_remote with :update option, it expects
HTML back (not javascript). Try removing :update option from link_to_remote.

That works. Thank you.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs