Take the 2-minute tour ×

I have a Rails controller in which I am setting a instance variable -

@user_name = "Some Username"

In my .slim template I am using coffee engine to generate javascript and want to print out the user name from client-sie javascript code -

  $(document).ready ->
    name = "#{@user_name}"
    alert name

But this is the javascript that is being generated??

$(document).ready(function() {
    var name;
    name = "" + this.my_name;

How do I access controller instance variables in my CoffeeScript code??

I am tagging this as haml since I am guessing haml will have the same issue when using CoffeeScript .

share|improve this question
Have you tried the fat arrow? $(document).ready => ? –  Thilo Nov 13 '11 at 0:04
What javascript would be right? I don't know rails, but I can translate js to coffee. –  thejh Nov 13 '11 at 0:04
@Thilo No, he's trying to inject the Ruby variable @user_name. Ruby and CoffeeScript use the same string interpolation syntax. –  Trevor Burnham Nov 13 '11 at 0:26
I see. BTW, how come the variable name changes from "#{@user_name}" to this.my_name ? –  Thilo Nov 13 '11 at 0:35
stackoverflow.com/questions/17560864/… Similar question/solution –  MrYoshiji Oct 23 '13 at 19:58

4 Answers 4

up vote 61 down vote accepted

What's happening is that "#{@user_name}" is being interpreted as CoffeeScript, not as Ruby code that's evaluated and injected into the CoffeeScript source. You're asking, "How do I inject a Ruby variable into my CoffeeScript source?"

The short answer is: Don't do this. The Rails team made an intentional decision not to support embedded CoffeeScript in templates in 3.1, because there's significant performance overhead to having to compile CoffeeScript on every request (as you'd have to do if you allowed arbitrary strings to be injected into the source).

My advice is to serve your Ruby variables separately as pure JavaScript, and then reference those variables from your CoffeeScript, e.g.:

  user_name = "#{@user_name}";
  $(document).ready ->
    name = user_name
    alert name
share|improve this answer
thanks for your reply, so basically what you have suggested is ok from performance perspective? –  user310525 Nov 13 '11 at 12:14
That depends on whether Slim recompiles the CoffeeScript in the template on every request. I'm honestly not sure. You should probably move it to an external file anyway; then it'll only be compiled to JS once, and browsers will cache it. –  Trevor Burnham Nov 13 '11 at 13:50

I tend to avoid inline javascript at all costs.

A nice way to store variables in your HTML, to be used from your javascript, is to use the HTML5 data-attributes. This is ideal to keep your javascript unobtrusive.

share|improve this answer
That wouldn't be helpful if you're using a JavaScript MVC and want to avoid making multiple HTTP requests to the server. backbonejs.org/#FAQ-bootstrap –  jackyalcine Jan 29 at 8:19
If you are using javascript MVC, you mostly do ajax requests to get the data, and render the html on the client anyway. But for instance, general config params you can still send once in data attributes in the html/body/container. –  nathanvda Jan 29 at 9:08
True, true. Sorry if I came off very rash! –  jackyalcine Jan 30 at 4:32

Here's a great Railscast episode about the general question of accessing Ruby variables inside of JavaScript/CoffeeScript files that you may find useful. http://railscasts.com/episodes/324-passing-data-to-javascript

share|improve this answer

You could also use:

$(document).ready ->
  name = <%= JSON.generate @user_name %>
  alert name

This is because JSON is a subset of JavaScript.

share|improve this answer

Your Answer


