I'm using Rails 3.2.13 and JavaScript in the /assets/javascripts/globals.js.erb. Is there anyway to access Rails helpers or controller data in the JavaScript file? Something like...
var App = {
globals: {
user: {
name: '<%= current_user.name %>'
}
}
};
you can't do that. the assets are compiled once in production so it shouldn't depend on the state of the request (like the current user, or the parameters passed to the request). The closest thing you can do is to add a global variable in your application layout
<script type="javascript">
App.globals.user.name = <%= current_user.name %>
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With