0

When I add my rails code into my jquery I have this problem:

Uncaught SyntaxError: Unexpected token <

Here my controller:

class WelcomeController < ApplicationController
    def index
        @a = "12345"
    end
end

My index.js

jQuery(function($) {
    $(document).ready(function($) {
        var a = <%= @a %>
        var url = "http://www.google.com"
        console.log(a)
    }); 
});

I don't know how to fix that, please help me!

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

1 Answers1

2

Change the filename to index.js.erb so that rails understands that you want to insert ruby code inside your javascript.

Also fix your javascript code:

jQuery(function($) {
    $(document).ready(function($) {
        var a = "<%= @a %>";
        var url = "http://www.google.com";
        console.log(a);
    }); 
});
karlingen
  • 13,800
  • 5
  • 43
  • 74