#400 What's New in Rails 4
Jan 04, 2013 | 20 minutes | Rails 4.0
Rails 4.0 is still unfinished, but it is shaping up to become a great release. Here I show how to setup a new Rails 4.0 (edge) application and walk through many of its new features.
- Download:
- source codeProject Files in Zip (42.4 KB)
- mp4Full Size H.264 Video (47.9 MB)
- m4vSmaller H.264 Video (23.6 MB)
- webmFull Size VP8 Video (24.5 MB)
- ogvFull Size Theora Video (52.5 MB)
Update 1/7/13: The mutatable scope methods (such as where!
) are intended to be used by Rails internals and are not part of the public API.
Resources
- Rails 4 Link Compilation
- Episode 342: Migrating to PostgreSQL
- Episode 345: Hstore (pro)
- Episode 390: Turbolinks (pro)
- Episode 371: Strong Parameters (pro)
- Episode 398: Service Objects (pro)
- Episode 379: Template Handlers (pro)
- Episode 387: Cache Digests
- Episode 280: Pry with Rails
- Episode 365: Thread-Safety (pro)
terminal
git clone https://github.com/rails/rails.git cd rails bundle gem install bundler --pre bundle railties/bin/rails new ~/code/blog --edge -d postgresql mate ~/code/blog cd ~/code/blog rake db:create rails g scaffold article name content:text published_on:date tags properties:hstore rake db:migrate rails c
rails console
Article.create! name: "Hello", tags: %w[ruby rails], properties: {author: "Ryan"} Article.first.tags Article.first.properties Article.all.class Article.scoped Article.all.to_a Article.all.load.class Article.none Article.where.not(name: "Hello") Article.order(name: :desc) as = Article.all; as.where!(name: "Hello"); # note this is only intended to be used by Rails internals as Article.find_by name: "Hello" Article.find_or_create_by name: "Hello" class Message include ActiveModel::Model attr_accessor :name, :content end m = Message.new(name: "Foo") m.name app.foo_url
db/migrate/create_articles.rb
execute "create extension hstore" create_table :articles do |t| t.string :name t.text :content t.date :published_on t.string :tags, array: true t.hstore :properties t.timestamps end
models/article.rb
scope :sorted, -> { order(:name) }
views/articles/_form.html.erb
<%= f.date_field :published_on %> ... <%= f.collection_check_boxes :tags, %w[ruby rails design], :to_s, :to_s %>
views/articles/edit.html.ruby
content_tag(:h1, "Edit") + render("form")
config/routes.rb
concern :commentable do resources :comments end resources :articles, concerns: :commentable resources :photos, concerns: :commentable get 'foo', to: 'articles#index', constraints: {protocol: "https", subdomain: "test"}
config/application.rb
# also need to add pry to Gemfile console do require "pry" config.console = Pry end