Rails3.2.2到Rails4.0.0升级笔记

Rails框架引入方式

在config/application.rb中进行引入

Rails3.2.2中,引入方式
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
Rails4.0.0中,引入方式
require 'rails/all'

配置相关

  • config.whiny_nils(将该项配置移除)

DEPRECATION WARNING: config.whiny_nils option is deprecated and no longer works. (called from block in

Rails被废弃的方法

1. instance_method_names

self.page_helpers = PageComponentHelper.instance_method_names

替换为

self.page_helpers = PageComponentHelper.instance_methods.map(&:to_s)

2. set_table_name

批量替换为self.table_name

The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option?
Rails::Paths::Path调用to_ary方法

3. 正则表达式

/Users/lisa/.rvm/gems/ruby-2.0.0-p598/gems/activemodel-4.0.0/lib/active_model/validations/format.rb:46:in `check_options_validity’: The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option? (ArgumentError)

/\A[A-Z0-9_]*\z/

解决方案:

If you don’t expect multiline incoming data, you need to replace all the “^” with “\A” and all the “$” with “\z”. If you expect multiline strings, just specify the multiline: true option. After that, you’re ready to go.

4. Gemfile

Rails 4.0 removed the assets group from Gemfile. You’d need to remove that line from your Gemfile when upgrading. You should also update your application file (in config/application.rb):

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

5. 路由

match必须指定via,否则报错
update更新操作的动词为patch(put)

6. ActiveRecord::ReadOnlyRecord

[4] pry(main)> Irm::Permission.query_by_function_code(function_code.to_s.upcase).destroy_all
ActiveRecord::ReadOnlyRecord: ActiveRecord::ReadOnlyRecord
from /Users/lisa/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/persistence.rb:155:in `destroy’

方法链中加入readonly(false)

7. reset_session

reset_session在Rails4中不可用,使用session.clear替代

8. 资源加载方式

  • Rails3.2
    group :assets do
        gem 'therubyracer', '0.12.2'
        gem 'uglifier', '>= 1.0.3'
        gem 'less-rails','2.6.0'
        gem 'twitter-bootstrap-rails' ,'3.2.2'
        gem 'jquery-rails','2.1.4'
    end
#/config/application.rb
if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end
  • Rail4.0.0
    gemfile中移除assets这个group,修改相应的配置文件(/config/application.rb)
        gem 'therubyracer', '0.12.2'
        gem 'uglifier', '>= 1.0.3'
        gem 'less-rails','2.6.0'
        gem 'twitter-bootstrap-rails' ,'3.2.2'
        gem 'jquery-rails','2.1.4'
    #/config/application.rb
    Bundler.require(:default, Rails.env)

9. attr_accessible、attr_protected被废弃

这两个方法最后可用的版本为rails3.2.13,用于在对model进行大量赋值时,通过指定白名单(attr_accessible)或黑名单(attr_protected)的方式,确保安全性。
attr_accessibleh和attr_protected区别的详细说明

在rails4中,对大量赋值的控制提升到了controller层,采用强参的方式进行限制,故这两个方法被废除。

class PeopleController < ActionController::Base
  # This will raise an ActiveModel::ForbiddenAttributes exception
  # because it's using mass assignment without an explicit permit
  # step.
  def create
    Person.create(params[:person])
  end

  # This will pass with flying colors as long as there's a person key
  # in the parameters, otherwise it'll raise a
  # ActionController::ParameterMissing exception, which will get
  # caught by ActionController::Base and turned into that 400 Bad
  # Request reply.
  def update
    person = current_account.people.find(params[:id])
    person.update_attributes!(person_params)
    redirect_to person
  end

  private
  # Using a private method to encapsulate the permissible parameters
  # is just a good pattern since you'll be able to reuse the same
  # permit list between create and update. Also, you can specialize
  # this method with per-user checking of permissible attributes.
  def person_params
    #列举白名单列表
    params.require(:person).permit(:name, :age)
  end
end

方法变更

1. delete_if

  • Rails3中delete_if执行之后改变接收者值

    need_delete_functions.delete_if { |f| f[:code].to_s.eql?(function[:code].to_s) }
    
  • Rails4中delete_if执行之后不改变接收者值,需要将结果显示赋值

    need_delete_functions = need_delete_functions.delete_if { |f| f[:code].to_s.eql?(function[:code].to_s) }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值