#282 Upgrading to Rails 3.1
Sep 05, 2011 | 8 minutes | Rails 3.1
It is incredibly easy to upgrade to Rails 3.1, but if you want to take advantage of the asset pipeline you will need to put in some extra effort. Have no fear because I walk you through each of the necessary steps in this episode.
config.assets.compile = true+
config.assets.digest = truesaved my day. Lost hours yesterday figuring out, why
stylesheet_link_tag 'application'had refused to link
The download links appear to be missing. Here's a link to the MP4 version.
How about images referenced inside of your css (or js) files? Is there a better solution than rename these files to
.css.erband using the
image_taginside of them?
image-urlasset helper. Check out https://github.com/rails/sass-rails
ah.. thanks for that. had been using filename.css.erb and asset_data_uri().
wasn't very happy with that solution.
Does Rails 3.1 can use the gem for rails 3.0 ?
Thanks as always Ryan for a great screencast. Are there any know gem incompatibilities with the new rails 3.1? E.g. We use refinerycms a lot and there it seems it is not compatiable out of the box yet.
You should be careful with that
bundle update- if you run it, not only will rails and its dependencies be updated, but also everything else. If you are not careful with the version numbers you specifiy in your Gemfile, you might end up with potentially breaking updates of the gems you're depending upon. I think for updating Rails it's better to first
bundle update rails. If everything works, go on and update all your other gems.
What are you planning to do for your 404.html and 500.html pages? I'm using the default asset pipeline settings (e.g. digests, don't allow fallback), and I use my application's CSS file inside those error pages.
The problem is, in production I don't know what the filename for that CSS file will be, since it's using a digest. And I want to keep my 500 page as just static HTML, without any processing on the server, so using the normal rails stylesheet link tags is out.
Any ideas? I hate to just copy the CSS I need...
Just for more info...what I'm doing at the moment is just referencing /assets/application.css from my error pages. This works; however:
It's unclear to me why it works; it seems rails has some sort of fallback if an asset isn't found in the public/assets directory. Which leads to...
This file gets served from rails (I can see it happening in the production.log file), rather than "normal" assets that get served from the web server from the public/assets directory that's created during asset precompilation. So, for example, if rails is totally hosed and throwing exceptions, my 500.html isn't going to work.
Just to follow up...turns out that currently, the rails asset server is mounted regardless of the "fallback" setting, apparently to allow for cases just like this. There is some discussion about it at
It looks likely this behavior will change in the future, but not yet sure how.
Why not simply move the images folder into the public directory? What's the downside of not having images go through the asset pipeline? Or rather, what's the benefit of using the asset pipeline for images too?
The main reason I can think of would be to always get the right path, regardless of which directory structure the app is installed in, but that seems like a relatively minor problem to have.
Well, a benefit of the asset pipeline is md5 fingerprinting of the images...
If you're not using ActiveRecord or you're skipping some other default Rails module, there's another gotcha in
application.rb. For example, you can cherry-pick which modules you want to use by requiring them explicitly:
If the top of your
require rails/all, make sure you add this line:
Thanks for the tip!
I just finish migrating one of my projects and also discovered that in rails 3.1 they deprecated RAILS_ROOT in favour of Rails.root,RAILS_ENV in favour of Rails.env, and
RAILS_DEFAULT_LOGGER in favour of Rails.logge
I made the changes and the project seems to be working properly.
Thanks for the info Ryan.
One quick question.. I was able to upgrade my application successfully, though I am facing 1 issue. When I run my app in the production mode (rails s -e production) for some reason my view helpers are not included even though I have helper :all in the application_controller.rb file.. Any hints as to why that is happening?
Not so fun for Windows using MySQL2. Version compiled for Windows breaks in 3.1 and latest version seems to have problems compiling as it is looking for a Makefile, which Windows does not use.
breaks in 3.1 and latest version seems to have problems compiling as it is looking for a Makefile, which Windows does not use.http://www.bagmulberry.com/mulberry-bayswater-bag-c-3.html
Does anyone know how to make sure the images loaded by jQueryUI library CSS are correctly referenced in the production?
I know from the one of the comments that you can use image-url('some_pic.jpg'); but that's simpler when you have written the code yourself, when you drop in a library from somewhere else, you have to hunt them all down the first time you use it and each time you upgrade it, that sounds real bad to me.
I am guessing this is a non-solvable problem at the moment. Because if it is solvable by some clever plugin, then what's the point of correcting all the url('dir1/dir2/image.jpg') to image-url('image.jpg')? But then including an external library that has images is not at all uncommon, I would think that the Rails team would have thought about it...
Folks, there's one gotcha more I stumbled upon. I had formtastic forms in my app and after upgrading they weren't working anymore. Turned to be as simple as putting = in front of the tags. Rails 3.0 left (although deprecated) the output of <% (or - in haml) but Rails 3.1 seems more strict (which is a good thing) and won't output that!
Tried to upgrade my blog from 3.0.9 to 3.0.10 and 3.1.0 like you did in the tutorial but I got the most stupid error ever with no proper message, so I have no idea whatsoever of what's going on:
Started GET "/admin" for 127.0.0.1 at 2011-10-10 12:28:33 +0300
ArgumentError (wrong number of arguments (3 for 2)):
Rendered c:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered c:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered c:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (10.0ms)
After editing all files and run
There will be an error on Ubuntu:
After i installed node.js
apt-get install nodejsit works.
Maybe too late, but i want to ask a question about my error. When i upgrade my application from 3.0.11 to 3.1.3 there is no problem.
But, i have to use double mysql server one of them is legacy database with version 4.0.
When i trying to connect this database, i got an error like that;
Mysql::Error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Key_name = 'PRIMARY'' at line 1: SHOW INDEX FROM
personel_giriscikisWHERE Key_name = 'PRIMARY'
ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Key_name = 'PRIMARY'' at line 1: SHOW INDEX FROM
personel_giriscikisWHERE Key_name = 'PRIMARY'
But, in rails 3.0.11 there is no problem and i was making the query in the Mysql 4.0.
Any Idea, thank you.
Maybe a silly question but why the jquery-rails gem is not in the assets group?
One other thing to do: replace all occurrences of
<% form_for ...with
<%= form_for ....