Forum: Ruby on Rails NewbieQ: How do I refresh view after return from popup windo

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.
Eric S. (Guest)
on 2006-01-06 08:37
(Received via mailing list)
Hi,
I have this bitta code that is supposed to open a new window and allow
the user to create a new Organisation on the fly whilst editing a new
Person i.e. if the Organisation exists they can select it from the drop
down select. Otherwise they can click on the cute button and enter a
skeleton Organisation record. What I need to do next is refresh the drop
down select on returno the new organisation is shown and selected.

I have no idea how to do that - Any ideas? BTW - I'm not yet using AJAX


Usual Person data... Name, Position, etc
Then;
<td>
<%= link_to(image_tag("new_organisation.png", :alt => 'New',
	:border =>1),
	{ :controller => 'organisations', :action => 'new'},
	:popup	=> ['new_window', 'height=100,width=100']) %>
<select id="person.organisation_id" name="person[organisation_id]">
	<%= options_from_collection_for_select @org,
		"id", "organisation_name", @person.organisation_id %>		</select></p>
</td>
Victor (Guest)
on 2006-10-02 23:38
I would be very interested in this question too........has anybody
figured this out?

Thanks
Victor

Eric S. wrote:
> Hi,
> I have this bitta code that is supposed to open a new window and allow
> the user to create a new Organisation on the fly whilst editing a new
> Person i.e. if the Organisation exists they can select it from the drop
> down select. Otherwise they can click on the cute button and enter a
> skeleton Organisation record. What I need to do next is refresh the drop
> down select on returno the new organisation is shown and selected.
>
> I have no idea how to do that - Any ideas? BTW - I'm not yet using AJAX
>
>
> Usual Person data... Name, Position, etc
> Then;
> <td>
> <%= link_to(image_tag("new_organisation.png", :alt => 'New',
> 	:border =>1),
> 	{ :controller => 'organisations', :action => 'new'},
> 	:popup	=> ['new_window', 'height=100,width=100']) %>
> <select id="person.organisation_id" name="person[organisation_id]">
> 	<%= options_from_collection_for_select @org,
> 		"id", "organisation_name", @person.organisation_id %>		</select></p>
> </td>
Kad K. (Guest)
on 2006-10-03 01:51
I'd be too.. will try tomorrow.. with some Ajax request...

Victor wrote:
> I would be very interested in this question too........has anybody
> figured this out?
>
> Thanks
> Victor
>
Bill W. (Guest)
on 2006-10-03 02:04
(Received via mailing list)
> Eric S. wrote:

>> What I need to do next is refresh the drop
>> down select on returno the new organisation
>> is shown and selected.
>>
>> I have no idea how to do that - Any ideas?
>> BTW - I'm not yet using AJAX

You need to be using Ajax (probably via RJS).  Put the drop-down in a
<div>
and re-render it after you're done entering the new Org record.

To learn RJS, I strongly recommend Cody F.'s "RJS Templates" PDF
from
O'Reilly.  One of the best $10 I ever spent to learn a new programming
tool
/ technique.

hth,
Bill
Sjors (Guest)
on 2006-10-03 13:44
(Received via mailing list)
AJAX seems like a good option here. But if you don't want to get into
that and don't mind the whole page being refreshed you could also try
something like this:

- Add a hidden field to your form called "organisation_added" which is
set to false by default.
- Then when an organisation is added, use JavaScript to set
"organisation_added" to true and post the form.
- Then with Ruby, if params['organisation_added'] is set to true create
a person object using the data that has already been entered (@person =
Person.new(params[:person]) ), and re-render the form.
- params['organisation_added'] == false means the user posted the form
manually, so in that case you can save the person to your DB.

Good luck!
Ed W. (Guest)
on 2006-10-03 17:09
(Received via mailing list)
Hi

> - Then when an organisation is added, use JavaScript to set
> "organisation_added" to true and post the form.
>

How to do this bit?

In particular how to use javascript from the popup to reference the page
it was popped up from.  Also how to set the field and post the form
whilst not loosing any of the data already entered?

In my case I want to let the user enter som Lat/Lon position data, OR
for convenience popup a google maps object and have it fill in the
details in the original form when the user clicks on a location.  I
don't feel that I need to post the form, just figure out how to
reference back from the popped up page back to the original page?

Cheers

Ed W
Sjors (Guest)
on 2006-10-03 18:59
(Received via mailing list)
In Javascript window.opener is your reference to the parent window,
i.e. the window it was popped up from. Example:

In your main page where the form exists:
<input type="hidden" id="person_haircolour" name="person[haircolour]"
value="#FFFFFF"/><!-- This guy must be an albino! lol -->

In the pop-up page:
<input type="button"
onclick="window.opener.getElementById('person_haircolour').value =
selectedColour; window.close();" />
This topic is locked and can not be replied to.