Rails, jQuery, .js.erb files, JS not executed by the browser

后端 未结 1 1207
暖寄归人
暖寄归人 2021-02-14 06:49

I\'m trying to use jQuery, and everything has been great, until now, when I\'m trying to render a partial and append it to a div. Here is how I have it set up:

I have an

相关标签:
1条回答
  • 2021-02-14 07:45

    You have to call it from your view or it will never be executed.

    an example controller:

    def index
      @objects = Object.find(:all)
    
      respond_to do |format|
        format.js{
          render :text => "alert('hello')"
        }
      end
    end
    

    and an index.html.erb with:

    <script type="text/javascript">
      $(function(){
        $.ajax({ url: '/controller', type: 'get', dataType:'script' });
      });
    </script>
    

    replace '/controller' with the actual url that executes that controller's index action, by default for PostsController it will be '/posts' and so on...

    if you like rjs you delete the {} and everithing in it in the controller and create an index.js.erb or index.rjs with:

    page.alert("hello world")
    

    or:

    page << "hello world"
    
    0 讨论(0)
提交回复
热议问题