Ruby on Rails has changed a lot these few years. There are some security issues found recently. One of them is regarding the vulnerability in the JSON parser that comes with Active Support. You can read more here. However, the patch provided is only available for Rails 2.3.x and 3.0.x. I checked the code for Rails 2.2.2 and found that this version is affected as well. Without the official patch, I have to come up with my own patch for that. This is a serious problem if your application is not up to date with Rails. I developed a Ruby on Rails website that provides stocks information about three years ago with a small team, which stayed with Rails 2.2.2. Now it is time to upgrade it to the latest Rails 3.2.12. Below is how I did it and some of the major changes that has to be made in the code.
The whole project structure has changed quite a lot between Rails 2 and Rails 3 especially with the introduction of asset pipeline and bundlers. The easier way to upgrade your apps is to create a blank new Rails 3 apps and move in all the new folders into your old Rails 2 apps. For the files with the same names under same directories e.g. config/environments.rb, config/environments/production.rb, there are some API/format changes, so you need to compare the contents and see how to merge them. After you have done that, you can try to start your rails applications, which I believe the server can’t even be started. You should check every single error messages shows up in the console and fix them one by one until everything is fine. Below are some major changes that I met with.
- For the app/controllers/application.rb, it should be named as application_controller.rb now. Otherwise ‘uninitialized constant ApplicationController’ would be thrown.
- ENV['RAILS_ENV'] is now deprecated, use Rails.env instead.
- Rails.root class used to be String. Now it is changed to PathName. So you can’t do things like below:
File.read(Rails.root + "/config/streaming_config.yml")
- lib folders are not auto loaded. So you will see some missing constant errors. To auto load the lib folders add below two lines to the config/application.rb.
config.autoload_paths += %W(#{config.root}/lib) config.autoload_paths += Dir["#{config.root}/lib/**/"] - filter_parameter_logging is no longer available. To filter out the parameters you can do it in the config/application.rb.
config.filter_parameters += [:password]
- You cannot access controller methods in the view with @controller anymore. You have to use controller instead.
- For action view rendering, you no longer need to call h(string) to escape HTML output, it is on by default in all view templates. In Rails 2 you need to do below to escape the parameters, if not, you are vulnerable for XSS attacks.
<%= h @params[:user_name] %>
In Rails 3, this html escape is on by default. However if the variable or contents you are trying to render contains html, and you want to render the html you have to explicitly call raw methods or html_safe method.
<%= raw @page.content %>
<%= @page.content.html_safe %>
I forget to put raw or html_safe in some of the views, and it renders escaped html instead. So you may want to check across the whole site to make sure everything is alright.
- For form_tag and form_for, you need to use <%= %> instead of <% %>, otherwise the form won’t be rendered at all.
- will_paginate 2 won’t work with Rails 3. Have to upgrade to will_paginate 3 otherwise uninitialized constant ActiveRecord::Associations::AssociationCollection error will be thrown.
- Array.paginate will throw error.The Array#paginate method still exists, too, but is not loaded by default. If you need to paginate static arrays, first require it in your code: require ‘will_paginate/array’
- For active record, save(false) changed to
save(:validate => false)
- request.request_uri changed to request.url
- REXML::Document is not auto loaded, need to explicitly require it before using.
require 'rexml/document'
- rake API changes. The :needs => :environments is deprecated. In Rails 2 :
task :task_name, :argument_name, :needs => :environment do |t,args| # ... endIn Rails 3, you have to do the following:
task :task_name, [:argument_name] => :environment do |t,args| # ... end - params[:path] used to be an array of the path split by slash. e.g. you might see the value as ['user', 'details.html'], now it is a string /user/details. Note that params[:path] doesn’t contains the format.
- interpreate_status is not in use any more. You can use Rack::Utils::HTTP_STATUS_CODES[status_code] to do the same thing.
- Mailer API changes. In Rails 2, you would call deliver_welcome_email or create_welcome_email. This has been deprecated in Rails 3.0 in favour of just calling the method name itself. So you can call Mailer.welcome_email.
- Mail API changes. In Rails 2 you define a mailer like below (this example is copied from rails guide)
class UserMailer < ActionMailer::Base def welcome_email(user) recipients user.email from "notifications@example.com" subject "Welcome to My Awesome Site" sent_on Time.now body {:user => user, :url => "http://example.com/login"} end endHowever in Rails 3 above codes has to be changed to below:
class UserMailer < ActionMailer::Base def welcome_email(user) @user = user @url = "http://example.com/login" mail( :to => user.email :from => "notifications@example.com" :subject => "Welcome to My Awesome Site" :date => Time.now ) end end - If a action name is not defined in the controller but the corresponding views file exists, in Rails 2, it will call method_missing. However in Rails 3, it won’t call method_missings. Thus in the help controller we can’t use the method_missing to dynamic rendering the views.




