21

Big Update:

As I finally found the real solution, I also discovered the real problem. As I wrote down here a lot of useless information, considering the real problem, I'm making a huge update of the question so that other people can find easily what's going on and can see the solution.

The Problem: It's because of the assets pipeline of Rails 3.1

Actually... It's an easy one: the assets were rendered twice in development-environment. Doing lot of investigations shew me that my Rails 3.1 server was rendering the assets from both the "app/assets" and "public/assets" folders. So, I had every .js and .css files duplicated, which was breaking all my javascript animations (yeah... binding twice the same event and handler to the same element is not what you want... normally).

And if the problem appeared all of a sudden, that was because I had to run "rake assets:precompile" to deploy my application. Since that, when my application was running in development, the server was rendering the static precompiled assets and the dynamic precompiled assets.

The solution (there's now a better one few lines below) - but you can still read it

First one: I just had to delete all the precompiled assets from my public folder.

Better one: Add config.serve_static_assets = false to development.rb which will prevent loading files from /public/assets. Also, don't forget to reset your browser cache.

[Edit: July 20th 2012]

Advanced one: I recently had a new problem because of those static assets. You know, when you use paperclip or some other gem and they add your images in your public folder in some system sub-folder because it's better if you want to deploy your application using capistrano. Well, that's great but! As we added config.serve_static_assets=false, those images aren't rendered in development and that's bad if you want to do some css on them. So! What to do then?

Well in fact you'll have to turn on static assets in development like so:

# Expands the lines which load the assets
config.assets.debug = true
config.serve_static_assets = true

Then to prevent rails from rendering your other assets twice (the precompiled ones), just do this command:

rake assets:clean

It's the opposite of rake assets:precompile and will clean your public/assets folder so that Rails won't render your assets twice. Of course you'll still have to clean your browser cache and clean your assets each time you precompiled them.

[Edit: November 18th 2013] - From @idejuan answer

Another solution:

You can add this line:

config.assets.prefix = '/dev/assets'

To development.rb, where the prefix can be whatever you want. Scripts will not load twice anymore, and images in /public/system will be read! But be carefull as it changes the path to your "static" assets... if you require assets from a gem, it might not load them properly in development...

[End edit]

The remaining question with answer!

Well, why my development application was rendering static precompiled assets?

In fact if you precompile your assets localy, rails render assets from the public folder AND from the assets folder in development and test environment by default. Normally assets from the public folder should overwrite those from the assets folder, but it's not the case and even if it does, we would lost the benefits of the "debug_mode" as we would have to precompile assets each time. So... Assets are rendered twice when precompiled locally in development and test environment.

So, by adding "config.serve_static_assets = false" to your development.rb file, you somehow overwrite the default line that telling Rails to look in your public folder for assets. I hope they'll do something cleaner one day about locally precompiled assets.

Thanks to the ones who helped me for my investigations :).

Kulgar.

Kulgar
  • 1,855
  • 19
  • 26
  • 4
    No one can help you: you neglected to give any error messages or even tell us about the structure of your application. We don't even know if you're getting server-side or client-side errors! – Marnen Laibow-Koser Nov 17 '11 at 15:47
  • That's because I don't have any error messages to give. This is not a "classic error" where you get an error message. I wish I would have one to give, but I don't... The error is that the javascript is acting weirdly only when my Rails application is launched locally... – Kulgar Nov 17 '11 at 15:56
  • 1
    If it's acting weirdly, then it's doing something that it's not supposed to, or it's not doing something that it is supposed to. If neither of those is the case, then you don't have a problem. So...what is your problem? – Marnen Laibow-Koser Nov 17 '11 at 15:58
  • My problem is that it wasn't acting weirdly when I was on the previous version of Ubuntu... I added some information to my question. – Kulgar Nov 17 '11 at 16:00
  • "Acting weird" is not enough information. Concrete examples, please. – Marnen Laibow-Koser Nov 17 '11 at 16:01
  • Hmm... okay, here is the online application: http://www.ct2c.fr/ do you see the javascript animations at the home page? That what's acting weird locally, the animations is doing some strange stuff such as: reducing all the images (even the front one)... moving images in both directions... – Kulgar Nov 17 '11 at 16:02
  • Which JS animations, the slideshow? Yes, I see them. Again, how are they acting weirdly? Are you sure the code is identical in both instances of the application? Are you using the same browser? You're not providing any useful information -- you're just essentially saying "help it doesn't work fix it". I sympathize, but I can't help with that amount of info. – Marnen Laibow-Koser Nov 17 '11 at 16:06
  • But... I did explain that I used the same code, it's in my question: "So... I decided to download my uploaded sources and put them on my Ubuntu system. But when I launch the rails server, the weird javascript behavior happens again. So I'm sure it has nothing to do with my application code, as it works fine on the uploaded application and under a Windows environment." I'm using the same browser. I'm sure the problem appeared because I updated Ubuntu... – Kulgar Nov 17 '11 at 16:08
  • My mistake, missed that sentence. Sorry. Again, are you using the same browser in both cases? – Marnen Laibow-Koser Nov 17 '11 at 16:09
  • Yes I do, and it worked fine when I was on a previous version of Ubuntu... – Kulgar Nov 17 '11 at 16:11

4 Answers4

10

You might want to look at

https://stackoverflow.com/a/7854902/686460

"Adding config.serve_static_assets = false to development.rb will prevent loading files from /public/assets"

I think that is a better solution than what you are suggesting here.

Community
  • 1
  • 1
Agustin
  • 1,254
  • 13
  • 10
  • 2
    Thanks! It does work too! Of course I also had to do this: "It's a caching issue. reset your browser cache. Happened to me before I figured it out" which is also in the other post you're pointing at. – Kulgar Feb 20 '12 at 19:25
  • 1
    It happened to me as well. Browser cache was the problem. – channa ly Oct 18 '13 at 03:56
3

I had the same problem and found an easy solution/hack to it. I just added this line:

config.assets.prefix = '/dev/assets'

to development.rb, where the prefix can be whatever you want, I put /dev/assets to avoid further confusion.

Scripts do not load twice anymore, and I can use the images I have in /public/system that could not be read when I tried the solution of config.serve_static_assets = false

So far I have not found disadvantages to this solution. Hope it serves to you as well!

idejuan
  • 461
  • 2
  • 4
  • 11
2

I can't see why an Ubuntu upgrade would have anything to do with your JavaScript. Are you using static JavaScript? Dynamic JavaScript? CoffeeScript? If one of the latter two, I suppose it's possible that the upgrade broke one of the tools that's processing your JS...

Some things to try:

  • View the JavaScript source in your browser. Make sure that the same source code is reaching your browser.
  • Try running the Rails app on a different system (perhaps install a second VM with a different Ubuntu version and see if you can replicate the problem).
  • Make triple-sure that the exact same version of the app code is in both places.
Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33
  • Thanks, I'll try those things! :) – Kulgar Nov 17 '11 at 16:16
  • Okay... I made a new "edit" (edit 3). Sorry for my poor English... Do you think the problem may come from an update of one the gems (such as JQuery-rails)? – Kulgar Nov 17 '11 at 17:46
  • Perhaps -- compare your `Gemfile.lock`s and see if they're different. Are you getting any *JS* errors? (BTW, your English is fine.) – Marnen Laibow-Koser Nov 17 '11 at 18:04
  • Nop... I searched for any JS errors using firebug, but nothing came up... And well... after writing down the edit, I changed my gemfile, specifying the exact same version for each gem as the ones that are in the Gemfile.lock of my distant application. Nothing changed... I was trying something else before updating my question. – Kulgar Nov 17 '11 at 18:09
  • Did you `bundle` after changing your `Gemfile`? Did you compare `Gemfile.lock`s as I suggested (not `Gemfile`s)? Do you have decent version control in place? – Marnen Laibow-Koser Nov 17 '11 at 18:11
  • I have git in place ^^. I did the bundle. And, for Gemfile.lock comparison, there are some few differences for the following gems: "ansi, coffe-script-source, polyglot, tzinfo" and that's all... But how come it works fine if I launch the server with the "-e production" option? (I added my development config settings) – Kulgar Nov 17 '11 at 18:25
  • coffee-script-source might influence how CoffeeScript is being processed; I'm not sure. Also, I suspect that `config.assets.compress` might be at issue here. Try changing that setting and see what happens. – Marnen Laibow-Koser Nov 17 '11 at 18:49
  • I made a new edit :) Thank you so much for your help! Still... I don't understand why the "debug" option was the problem... – Kulgar Nov 17 '11 at 18:59
2

I would also suggest that you run the rails server passing it a different environment. This could be related to the Environment settings you have set up for development.

rails server -e production

If the problem disappears it has got to do with your config settings for the "development environment.

mithun_daa
  • 4,334
  • 5
  • 38
  • 50
  • Hmm ok, that's really weird... Because yeah, it works fine if I launch the server in the production environment. – Kulgar Nov 17 '11 at 18:04
  • Either post you prod config here if you can, or just compare, maybe even replace you dev config with the prod config just to rule out the possibility that it is the config. – mithun_daa Nov 17 '11 at 18:49
  • I made an edit, I found the solution! Thank you so much to both of you! ^^ That's weird, apparentely the debug option must be set to false by default in development.rb, but it is not... And that was the source of the problem, apparently. That's weird, isn't it? – Kulgar Nov 17 '11 at 19:00
  • Why is it weird? Apparently, your assets -- in this case, your JS -- are relying on being compressed in order to function properly. That's *bad*. Go through the JS in Firebug or something and find out what's going on. – Marnen Laibow-Koser Nov 17 '11 at 19:03
  • It's weird because I never had to change that option before and I never changed my JS files, and everything was working fine. So, why it's breaking now... That's what I find weird ^^. – Kulgar Nov 17 '11 at 19:11