views:

43

answers:

1

I have a form which contains rows like this one. I need to be able to call a method on a controller using ajax/jquery when the user clicks the radio button. The method would change the underlying model, on return it would show a message in the flash on the client.

<tr>
  <td><%= radio_button_tag 'primary', feature_photo.id %></td>
  <td>
    <%= feature_photo.name %>
    By: <%= feature_photo.photo_credit %>
  </td>
<tr>
A: 

You're looking for observe_field. The rest is as simple as any other rails action (an action in a controller that returns something relevant whether that's text, javascript, etc).

You also mention jQuery, but it's not obvious how you're using it in your app. observe_field will generate Prototype code, not jQuery. If you've got some jQuery you can just write a handler for the radio button, perhaps like so:

$('#primary').bind( 'change', function() {
  $.ajax( {
    url: 'some_rails_action',
    data: $(this).is(':checked'),
    success: function( r ) {
      // do something with the response from rails here
    }
  } );
} );

With more details I might be able to provide more comprehensive help :)

thenduks
Thanks, jquery doesn't have observer_field!
rordude
@rordude, that's actually not true! Checkout jRails (http://github.com/aaronchi/jrails) for some 'ports' of the Prototype stuff built into Rails :)
thenduks