从rails3中的javascript执行控制器/动作

时间:2011-01-21 01:31:53

标签: javascript ruby-on-rails ajax ruby-on-rails-3

我的_new.html.erb中有以下功能。我想在更改element_id_placeholder时调用控制器:

 <script type="text/javascript">

    // When DOM loads, init the page.
    $(function() {
      // Executes a callback detecting changes with a frequency of 1 second
      $("#id_element_placeholder").observe_field(1, function( ) {                                       
        alert('Change observed! new value: ' + this.value );
        // call controller 

      });
    });

  </script>

我正在使用rails 3

1 个答案:

答案 0 :(得分:5)

使用jQuery的ajax方法:

$.ajax({
type: "GET",
url: "/articles/" + $(this).attr('value'),
success: function(data){}
});

http://api.jquery.com/jQuery.ajax/