2

I have a problem with Rails and Jquery. Im using AJAX to add comments to articles without reloading them. The following code got automatically included in my views/application.html:

  <%= javascript_include_tag "application" %>
  <%= javascript_include_tag :all %>
  <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" %>
  <%= javascript_include_tag  "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" %>

Everything seemed to work fine, until i realised that the server console shows the following error:

ActionController::RoutingError (No route matches [GET] "assets/all.js")

So since this line doesn't seem to add anything to the application other than an error i deleted it. Next time I started the server and used the application all of a sudden every comment gets posted twice!? Otherwise everything still worked fine. So I added the deleted line again and I have no idea why but when I add the line

<%= javascript_include_tag :all %>

again everythings works fine again, only one comment gets posted as intended. However I dont want to keep this in the code since it throws an error. Can someone explain this behaviour and tell me how to fix this?

2
  • What's the Rails version? 2.3 or 3.0 or 3.1? Notice that Rails manages assets with assets pipeline since 3.1. And the default application.js has jquery and jquery-rjs included by default. But the routing error seems strange :( Commented Dec 6, 2011 at 17:57
  • I have rails (3.1.0). so should i delete everything but "application" ? However in my application.js everything is commented out..? Commented Dec 6, 2011 at 18:38

1 Answer 1

5

Rails 3.1 uses sprockets to bundle javascript and css files. This makes the :all option deprecated. Sprockets use 'magical' comments to manage which javascripts are included.

So your application.js should look something like this:

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .

The first line also includes jQuery itself, so you don't need the other script tags. All you need to to is to point to application.

<%= javascript_include_tag "application" %>

If you're deploying to production, you'll need to run rake assets:precompile.

There is a Railscasts episode on assets, which is a must see.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.