markdown 升级Rails

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 升级Rails相关的知识,希望对你有一定的参考价值。

## Ugrading from 4.2 to 5.0
* **Rails 5.0 requires ruby version `2.2.2+`**
  * Current version: `2.3.1` 
* All models inherit from `ApplicationRecord`
  * **Create `app/models/application_record.rb` with the following code**:
  ```ruby
  class ApplicationRecord < ActiveRecord::Base
    self.abstract_class = true
  end
  ```
  * Make sure all your models inherit from it!
* `before` callbacks would return `false` in ActiveRecord and ActiveModel and thereby halting the entire execution chain.
  * Callback chains must be explicitly halted by calling `throw(:abort)`
  * **Add to `config/application.rb: ActiveSupport.halt_callback_chains_on_return_false = false`**
* `ActiveJob` now inherits from `ApplicationJob`
  * **Create `app/jobs/application_job.rb`** add the following code
  ```ruby
  class ApplicationJob < ActiveJob::Base
  end
  ```
  * Make sure all job classes inherit from it.
* **If using `assigns` and `assert_template` in controller tests add `gem 'rails-controller-testing'` to gemfile**
* Uploading files using `ActionDispatch::Http::UploadFile` replace with **`Rack::Test::UploadedFile`**
* Autoloading is now disabled after booting in Production Environment, if need to be set use: `Rails.application.config.enable_dependency_loading = true`
* `ActiveModel::Serializers::Xml` has been extracted into a gem: `gem 'activemodel-serializers-xml'`
* Support for `mysql` has been removed, use `mysql2`
* **Use `rails` instead of `rake` for running tasks and tests**
* Calling `params` will now return an object instead of a hash.
  * If using `params.permit` then no changes are needed.
  * Otherwise, you will need to convert `params.permit([:proceed_to, :return_to]).to_h`
* `protect_from_forgery` defaults to `prepend: false`
* Default template handler is now `RAW`. Always specify the extension of your file to be parsed by an appropriate template handler
* Add wildcard for template dependencies
* **`content_tag_for` and `div_for` have been removed in favor of just using `content_tag`, to continue using the older view helper methods
add the gem `gem 'record_tag_helper', '~> 1.0'` to gemfile**
* The `protected_attributes` gem is no longer supported in Rails 5!
* The `activerecord-deprecated_finders` gem is no longer supported in Rails 5!
* **Test cases are now ran randomly instead of sorted, to revert to sorted:** 
```ruby
# config/environments/test.rb
Rails.application.configure do
  config.active_support.test_order = :sorted
end
```
* `ActionController::Live` became a concern!
  * If `ActionController::Live` is included in another module that is included in your controller, then you should extend the module with `ActiveSupport::Concern`

#### Framework Defaults
* `belongs_to` will now trigger a validation error **by default** if there is no association present
  * Can be turned off **per association** by using : `optional: true`
  * For all associations, place LOC inside an initializer: `config.active_record.belongs_to_required_by_default = true`
* Per-form CSRF tokens: when activated every form in your app will each have their own CSRF token that is specific to that
action and method for that form: `config.action_controller.per_form_csrf_tokens = true`
* For additional CSRF protection, you can configure your app to check if the HTTP `Origin` header should be checked against
the site's origin: `config.action_controller.forgery_protection_origin_check = true`
* Default mailer queue name is `mailers`, you can change it with this configuration: `config.action_mailer.deliver_later_queue_name = :new_queue_name`
* Support for fragment caching in your config: `config.action_mailer.perform_caching = true`
* Control how the schema is dumped: `config.active_record.dump_schemas = :all`
* Configure SSL options to enable HSTS with subdomains: `config.ssl_options = { hsts: { subdomains: true } }`
* Ruby 2.4: allows you to preserve the timezone of the reciever: `ActiveSupport.to_time_preserves_timezone = false`
* It is recommended that you do not set columns equal to a `String`, but pass a `Hash` instead, which will be converted to and from a JSON string automatically































## Ugrading from 5.0 to 5.1
* If app uses `HashWithIndifferentAccess` class, move to using `ActiveSupport::HashWithIndifferentAccess`
* In `config/secrets.yml` all keys are now loaded as **symbols**, using strings should be changed!

## Ugrading from 5.1 to 5.2
* Rails 5.2 adds bootsnap gem (which reduces boot time of app)
  * `app:update` sets it up in boot.rb, use it then add to gemfile
  * No need to use it then change the `boot.rb`
* Expiry info is imbedded in ecrypted or signed cookies, which makes the cookies incompatible with Rails older than 5.2
  * If you require cookies to be read by 5.1 and prior: `Rails.application.config.action_dispatch.use_authenticated_cookie_encryption: false`  
## Updating Steps
1) Update ruby version to be greater than 2.2.2
2) change the rails version in yor gemfile: `gem 'rails', '>= 5.0.0.rc2', '< 5.1'`
3) Run `bundle update rails`
4) Update dependency gems
5) Update binaries and configrations: `rails app:update`
6) Run `rails db:migrate`
7) Update changes mentioned in the update files

以上是关于markdown 升级Rails的主要内容,如果未能解决你的问题,请参考以下文章

markdown Rails新

markdown Ruby on Rails Cheatsheet

markdown Rails缓存

markdown 如何部署Rails应用程序

markdown Rails和Backbone.js设置

markdown Rails命令