views:

79

answers:

4

I'm starting to feel crazy because what I want to do is so simple: write a line of javascript in my Rails application.js file that will cause a script somewhere else in the app to execute. I've tried it with jQuery.get() and jQuery.getScript() but nothing seems to work.

Here's some sample code from my application.js file:

#application.js
$(document).ready(function() {
  $(".button").click.function(){
    $.get($(this).attr("href"), null, null, "script");
  });
});

or

#application.js, excerpt
  $.getScript("index.js.erb", function(){
    alert('script has run');
  });

among other variations. I've tried putting the index.js.erb file in the same views folder as my current page, or in the javascripts folder, and even tried making it a pure .js file. The script itself can't be the problem, I'm trying it with lines as simple as:

#app/views/index.js.erb)
$('.header').text('new text')

Nothing's working for me, please help!

A: 

Here's what I would check to get started:

  • Any errors in firebug?
  • Check the source of your page, does it include jQuery?
  • What if you just write an alert in application.js, does that work?
  • Are you sure you have the code you posted within a $(document).ready(...) callback?
thenduks
There's a ton of other working jQuery in the application, and when I write an alert, or the line of code I want to execute, in the application.js file it works fine. And everything in the application.js file is wrapped in a $(document.ready(...) callback.
kateray
Ok, so try running the same code in Firebug and see if it throws any errors that way.
thenduks
Also, do you see the request to the javascript file in your logs? Does the request ever get sent at all (again, with firebug)?
thenduks
Strange I get this error: 304 Not Modified 217ms that links to a line in my jquery.js file
kateray
A: 

Needs to be put in a:

$(function(){
 // your code goes here
});

block

brad
Yes, it is. Updated above.
kateray
A: 

Your web server will serve any *.js files located in the public/ directory. It will not serve any static files that are not in public/ and it will not serve any *.erb files even if they are in public/. Moreover, public/ directory is for static files only, and the web server won't run any *.js located within public/ through Erubis.

Instead, you want a separate route controller/action and route for your special ERB JavaScript file, and the ERB JavaScript file should be in the app/views/.

Justice
Thanks, I did try it with my index.js.erb file in the same views folder as my index.html.erb file. What do you mean about a 'separate route', though?
kateray
+1  A: 

Looks like you have an extra quote before "href".

Also, you might try adding the .js extension after href: ...attr("href") + '.js', ...

Tim
Check out the jQuery Railscast: http://railscasts.com/episodes/136-jquery
Tim
Thanks, the extra quote wasn't the problem. And I know that railscast by heart at this point -that's part of why this is so frustrating to me.
kateray
Maybe try changing the javascript/ajax call from "index.js.erb" to "index.js", but leave your file on the server as "index.js.erb"
Tim
Also remember to have a "format.js" line in your index action
Tim
Wow, okay, thanks. The problem was in the format.js line - the page was actually being rendered from another controller, but I changed the controller to specify where the javascript file was and it worked!
kateray