The right way to include page-specific JavaScript in Rails

后端 未结 5 785
青春惊慌失措
青春惊慌失措 2021-01-03 06:50

I want to include this, for example:

jQuery(document).ready(function($) {
    $(\'#my-modal\').modal(options)
});

In one specific place in

5条回答
  •  佛祖请我去吃肉
    2021-01-03 07:29

    Late to the party, but for anyone viewing this today I think I've come up with something better.

    Punchbox Gem for Rails

    With this, you can write code like:

    class Post {
      controller() {
        // Runs on every action
      }
      index() {
        // Runs on just the index action
      }
    }
    
    Punchbox.on('Posts', Post);
    

    (You can also create an object instead of a class. See the docs)

    I've written about this much more in depth here: https://kierancodes.com/blog/page-specific-javascript-in-rails-4

提交回复
热议问题