Ruby on Rails Thursday, March 31, 2011

On Mar 31, 2011, at 7:10 AM, Tobias H. wrote:

> Hey guys,
>
> i've the following question:
> how can i parse the value of a selectbox within a div-tag (with the id
> "ma") via AJAX and use the parsed value for reading out some data from
> my database. the parsing request should be started by selecting
> another
> selectbox.
>
> My mainquestion is: how can i parse a selectbox (if possible with rjs)
> an get the value in ruby (not just in javascript) for further actions
> (like reading out some data from database)?

You have to have JavaScript in your page which will respond to the
'change' event on your selectbox by sending an Ajax request to your
server. At that point, it will be a normal Rails request, and you'll
have the values. Your server will respond with the result. There are
helpers (or used to be in Rails 2) to generate this JavaScript for
you, but it's important to understand that you can't (from Ruby, in
Rails) ask the browser anything. You have to be waiting for it to ask
things of you.

The Prototype-flavored JavaScript to do this would be something like
this:

$('mySelectBoxId').observe('change',function(evt){
new Ajax.Request('your/server/endpoint',{
parameters:{mySelectBox:this.getValue()},
onSuccess:function(transport){
//do something with
transport.responseText
},
onFailure: function(transport){
//sad face
}
});
});

You can probably get similar code injected into your page for you by
one of the Rails helpers, but that's the basic mode by which it will
work.

Walter

>
> thank you for your response!
>
> Tobe
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-
> talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
> .
>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment