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?
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:
class PersonController < ApplicationController
def add_address @person = Person.find(params[:id]) @person.person_addresses.create(:address =>
Address.create(params[:address]))
end
end
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
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.
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.
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.
@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?