jeudi 3 mars 2016

share/require Rails controller specific CoffeeScript between HTML and JS views

Rails controller specific javascript is enabled in layout.

<%= javascript_include_tag params[:controller] %>

Now let's create one named app/assets/javascript/hello.coffee to show what's question.

class Hello
  constructor: ->
    console.log("Hello world")
$ ->
  new Hello

It should work great while loading /hello.html. Now there is an ajax request to the same Hello controller index action, it should render the .js view, which requires some common but it is controller specific CoffeeScript code.

To avoid duplicate code, I want to require it in the hello.coffee view file like this,

#= require hello
new Hello

But this simply doesn't work. How to do this in a proper/rails way?

Aucun commentaire:

Enregistrer un commentaire