Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing current_user helper in JavaScript

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 %>'
    }
  }
};
like image 539
Zeck Avatar asked Mar 19 '13 02:03

Zeck


1 Answers

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>
like image 85
jvnill Avatar answered Oct 19 '22 19:10

jvnill