Skip to content

Instantly share code, notes, and snippets.

@frankie-loves-jesus
Last active August 29, 2015 14:03
Show Gist options
  • Save frankie-loves-jesus/8881bd34fe680af5d553 to your computer and use it in GitHub Desktop.
Save frankie-loves-jesus/8881bd34fe680af5d553 to your computer and use it in GitHub Desktop.

How does one run a function from application.js inside a .js.erb file?

I'd rather not have to duplicate said function.

DOESN'T WORK

app/assets/javascripts/application.js

$(document).on('pagecontainershow', function() {
  function someOtherFunction() {
    console.log('Hello world')
  }
  
  // ...
});

app/views/articles/create.js.erb

<% unless @article.errors.any? %>
  someOtherFunction();
<% end %>

WORKS

app/assets/javascripts/application.js

function someOtherFunction() {
  console.log('Hello world')
}
  
$(document).on('pagecontainershow', function() {
  // ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment