Best practices for loading page content via AJAX request in Rails3?

后端 未结 4 481
醉酒成梦
醉酒成梦 2021-01-31 21:35

Suppose you have a page with two columns of content. For whatever reason, you wish to retrieve the HTML content of one of those columns after the page has loaded, via an AJAX re

4条回答
  •  太阳男子
    2021-01-31 21:51

    If you want to use the approach the OP proposed, I would recommend moving it to a separate application.js file and use unobtrusive JS. I wouldn't recommend putting the JS inline on the page itself.

    # index.html.erb
    
    # application.js $(function() { $.ajax({ url: "...", success: function (html) { $("#content").append(html); } }); }); # controller.rb # empty placeholder def index end

    This fires off the ajax request automatically without you having to put it on every page. It's better practice. I wouldn't call it optimal, but slighty m

提交回复
热议问题