【问题标题】:execute controller/action from javascript in rails3在rails3中从javascript执行控制器/动作
【发布时间】:2011-06-12 21:05:20
【问题描述】:

我的 _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

【问题讨论】:

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


    【解决方案1】:

    使用jQuery的ajax方法:

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

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

    【讨论】:

    • 谢谢它的工作,但它去我的控制器/视图。我将如何调用一些方法来返回模型对象,甚至是我可以作为该方法的一部分构建的 json 对象?
    猜你喜欢
    • 1970-01-01
    • 2013-08-16
    • 2018-02-07
    • 2013-02-14
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    相关资源
    最近更新 更多