Pages

Monday, 16 November 2015

Upgradation rails 2 to rails 4 (Errors)

Some Errors are given below while migration of rails 2 to rails 4
  • 'rake/rdoctask' is obsolete and no longer supported. Use 'rdoc/task' (available in RDoc 2.4.2+) instead.
require 'rake/rdoctask'
to
require 'rdoc/task'
  • undefined method `attr_accessible' for
Add ‘gem 'protected_attributes' to Gemfile
  • undefined method `link_to_function' for
Add patch in app/helpers/link_to_helper_function.rb

module LinkToFunctionHelper def link_to_function(name, *args, &block) html_options = args.extract_options!.symbolize_keys function = block_given? ? update_page(&block) : args[0] || '' onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;" href = html_options[:href] || '#' content_tag(:a, name, html_options.merge(:href => href, :onclick => onclick)) end end
  • ActiveRecord::AssociationNotFoundError (Association named 'authorizations' was not found on User
Change in lib/authenticated_system.rb:108 to (if using restful-authentication)


User.includes(:roles => :authorizations).find_by_id(session[:user_id])

  • undefined method `has_strings' for
Comment has_string wherever you find it.

  • Unknown key: :order. Valid keys are: :class_name, :class, :foreign_key…
Change format something like that


has_many :commission_reports, :order => "volume_year DESC, volume_month DESC", :dependent => :destroytohas_many :commission_reports, -> { order "volume_year DESC, volume_month DESC" }, dependent: :destroy
  • undefined method `on' for #<ActiveModel::Errors
Rails 4, the problem is that there's no "on" method for the Errors class anymore. We use "get". From

Change format something like that
@user.contact.errors.on(:attended_before)
 to
@user.contact.errors.get(:attended_before)
  • undefined method `call' when doing a simple query
Change scope from all models from

scope :enquiry, where({:involvement => INVOLVEMENT_ENQUIRY, :archived => false})
to
scope :enquiry, -> { where({:involvement => INVOLVEMENT_ENQUIRY, :archived => false}) }
  • Fix Typical Syntax on has_many like associations 
has_many :prices, -> { includes(:country).order("created_at desc") }, as: :priceable, dependent: :destroy, class_name: "Price"
  • Undefined method confirmed_me?
remove :confirmable from model where we have defined the devise parameters eg User model if not using
  • Unknown key: :include
Change has_many statement


  • NoMethodError (undefined method `authenticated?' )

FInd authenticated?  and replace with devise method valid_password?

  • NoMethodError (undefined method `forget_me' for
Copy forget_me method from restful-authentication to User model
  • link_to_function and the view helper with postfix _tag doesn’t work in rails 4 so the solution is
Add  Gem  into the gem file
gem 'prototype-rails'
In rails 2 prototype was supported but in rails 3 the jquery supports so we get the errors like
  • Keep in mind that the rails 2 already create a application.js and application.css in public folder so do not create same name files in assets folder. That can also give you conflicts.
  • element.dispatchEvent is not a function
Go to layout and check application layout


Here you have include the app/assets and public/assets together

So keep in mind the hierarchy of inclusion of files, in short
first jquery
second prototype
referred link
http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/

<script src="prototype.js"></script>
<script src="jquery.js"></script>

After that include below in head of the html file the below line
<script>jQuery.noConflict();</script>
  • javascript_include_tag :default doesn’t work in rails 4
  • calendar_date_select doesn’t work in rails 4
  • LocalJump Error
This is due to the return keyword written in block. So remove return from the block.
  • undefined method `scoped' for
scoped method has been deprecated from rails 4. use all on place of it.
  • Search Or.. functionality was not working so change it that code
Replace div with some some id
  • Move all the assets from public folder to app folder