remote_function :with 3
<%= select_tag "content_type", options_for_select(content_type_options),
{ :onchange =>
remote_function(
:url => {:action => 'filter_by_content'},
:with => 'Form.Element.serialize(this)') } %>The above code allows to submit via javascript the selected value of a combo box. The remote call is triggered via the onchange event handler of the select tag. Notice the parameters to the remote function call :url, as usual, but also the :with parameter. The :with option can be very useful if you need more control on what data needs to be sent to the server. In this case we use Form.Element.serialize that url encodes all the parameters found in a given div (doesn’t need to be a form). In this case the content_type=snippet is passed to the server.
Hi,
If you want to send a value from an arbitrary field element (instead of “this”) you can use:
:with => “{name: Field.getValue(‘field_val’)}”
Where ‘field_val’ is the value of any Field on the page. (Of course you’d want to URIEncode this if field_val is user-entered text)..
Let’s say the field “field_val” has “foo” in it – you’d end up posting this key/value pair in this example:
name=foo
Thanks for the article..
Steve
Thanks for the tip, Steve.