RJS and drop down menu

Trying to update a text field on a page with the value from a drop down
select menu. Having a hard time accessing the value in the menu. Any
help would be great! :slight_smile:

Hi Matt,

On Thu, 2009-06-25 at 18:31 +0200, Matt Y. wrote:

Trying to update a text field on a page with the value from a drop down
select menu. Having a hard time accessing the value in the menu. Any
help would be great! :slight_smile:

The basics are to put an observe_field on the drop down with a url that
does whatever processing you need to do to construct the update you want
to do and either a) use the :update option for observe_field, or b) use
RJS to do the update. The :update option limits you to a single DOM
element in terms of what can be updated. Say more to get more :wink:

HTH,
Bill

Bill,
I have a collection select that shows a list of users:
<%= collection_select(:person, :convert, @nonUsers, :id, :fullName,
{:prompt => ā€œChooseā€¦ā€}, :style => ā€œwidth:125px;ā€) %>

And a text field:
<%= text_field_tag :userName %>

When a user is selected Iā€™m trying to pull that user out of a array
variable that I already have set up, @nonUsers (used to populate the
collection_select), and have that userā€™s userName put into the text box.

I canā€™t seem to figure out how to access the value of whatā€™s been
selected with an observe_field.

(Trying to do all this with RJS and not make a post back to the server).

My observe_field looks like:
<%= observe_field ā€œperson_convertā€, :function => update_page {|page|
page[ā€œuserNameā€].value = @nonUsers.select{|p| p.id == ***** }.collect{
|p| p.fullName } }%>

BUT Iā€™m not sure what to put in place of the ***** to get that
collection_selectā€™s selected value.

Hi Matt,

I havenā€™t used the :function option before. I find it easier just to
write the JS when I want to keep it all in the browser, so take what
follows with a grain of salt.

On Thu, 2009-06-25 at 19:10 +0200, Matt Y. wrote:

collection_select), and have that userā€™s userName put into the text box.
Thatā€™s not what you want to do. observe_field will capture / pass
selected value.

I canā€™t seem to figure out how to access the value of whatā€™s been
selected with an observe_field.

(Trying to do all this with RJS and not make a post back to the server).

Personally, Iā€™d do it against the server first (because itā€™s
oh-so-simple) and leave the optimization until performance is an issue.

My observe_field looks like:
<%= observe_field ā€œperson_convertā€, :function => update_page {|page|
page[ā€œuserNameā€].value = @nonUsers.select{|p| p.id == ***** }.collect{
|p| p.fullName } }%>

BUT Iā€™m not sure what to put in place of the ***** to get that
collection_selectā€™s selected value.

If Iā€™m reading the api / code for observe_field right, Iā€™m not sure
the :function variation supports what you want to do. As best I can
tell (having not much time to review the Rails source), if it does, the
final form would, I think, look something like this:

<%= observe_field(ā€œperson_convertā€, :function => ā€œupdate_page(*****)ā€}%>

But in the build_observer source, it doesnā€™t look like thereā€™s a way to
pass the value in. Looks like itā€™s just passing the
ā€œupdate_page(*****)ā€ without any further processing.

I know thatā€™s not much help. Sorry. My recommendation would be to
repost your problem under a more descriptive Subject line. Something
like ā€˜problem passing selected value to observe_field :functionā€™

Best regards,
Bill