Skip to content

Instantly share code, notes, and snippets.

@wangzuo
Last active August 29, 2016 07:13
Show Gist options
  • Save wangzuo/485fac9064529ba8696947b3dfbc2d66 to your computer and use it in GitHub Desktop.
Save wangzuo/485fac9064529ba8696947b3dfbc2d66 to your computer and use it in GitHub Desktop.

Rails 5

ruby

  • Rails 5 requires Ruby 2.2.2 or newer. (2.2.4 or 2.3.0)
  • reduce memory usage by taking advantage of GC
  • gem update --system

ApplicationRecord && ApplicationJob

  • ApplicationRecord
  • ApplicationJob
  • ApplicationMailer
  • ApplicationController
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end
# app/jobs/application_job.rb
class ApplicationJob < ActiveJob::Base
end

bin/rails

  • rails db:migrate
  • rails styleguide:build

Controller changes

render nothing: true

  • deprecated :nothing option for render()
  • head :ok
  • head :not_found
  • head :unauthorized
  • head :internal_server_error

redirect_to :back

  • RedirectBackError
  • redirect_back(fallback_location: root_path)
  • rails#22506
  • required fallback_location argument
# app/controllers/application_controller.rb
def redirect_back(fallback_location: request.referer || root_path, **args)
  super(fallback_location: fallback_location, **args)
end

params not inherited from hash

  • rails#20868
  • params.to_h & params.to_unsafe_h
  • DEPRECATION WARNING (deprecated in rails 5.1)

Deprecated *_filter

  • skip_before_filter -> skip_before_action
  • before_filter -> before_action

Test

get :show, id: funding.id, format: :json # before
get :show, params: { id: funding.id, format: :json } # now

Others

pluck with enum status

image_tag nil check

db/schema.db

  • rails db:migrate
  • add_index -> t.index

or() with relation

Funding.where(status: 0).or(Funding.where(role: 0)).to_sql
SELECT \"fundings\".* FROM \"fundings\" WHERE (\"fundings\".\"status\" = 0 OR \"fundings\".\"role\" = 0)

belongs_to required by default

config.active_record.belongs_to_required_by_default = false

has_secure_token

class UserPitch < ApplicationRecord
  has_secure_token :token
end

# rails g model user_pitch token:token
create_table :user_pitches do |t|
  t.string :token
  t.timestamps
end

add_index :user_pitches, :token, unique: true

up = UserPitch.create
up.token
up.regenerate_token

Turbolinks

Rails 5.1

Sprockets 4 (not yet)

//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment