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!
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!
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
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