#400 What's New in Rails 4
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)
If you'd like to read more about Turbolinks check http://geekmonkey.org/articles/28-introducing-turbolinks-for-rails-4-0
Thanks Fabian
Hi Ryan! Do you feel that there is any discernable cost to getting started writing a new Rails 4 application now and updating it as opposed to waiting for the release? My concerns wouldn't involve many gems. I'm thinking more along the lines of application structure, features getting dropped, or things like that.
You make the changes in big releases easy to understand, thanks!
What I don't quite understand is the move of protected attributes from the model to the controller:
There's probably something I'm missing, so if someone could enlighten me then I'd be grateful.
Makes more sense after reading this:
http://rubysource.com/rails-4-quick-look-strong-parameters/
Some of repetition that I fear when I see code like this:
def book_params
if current_user && current_user.admin?
params[:book].permit(:name, :author, :public)
else
params[:book].permit(:name, :author)
end
end
Can be reduced be creating constant (named by role?) within the class:
def book_params
if current_user && current_user.admin?
params[:book].permit(User.attrbutes_assignable_by_admin)
else
params[:book].permit(User.attrbutes_assignable_by_non_admin)
end
end
`
And though I understand that this a better placement of state logic, still seems a bit cumbersome.
The whitelist lets me say all the attributes that are permitted unless a specific role was in use. It isn't often, but when it is, it's explicit. This forces me to specify the user role every place I'm acting on an attribute via a controller. And with the increase in times I have to remember to do something the more times I'm likely to miss something an create a security hole.
Again, I'm happy to be enlightened.
Ryan covered this in episode 371: Strong Parameters
I tried following along but I'm having problems with hstore. I've upgraded to the latest version of postgres with homebrew, but rake db:migrate complains about hstore not being a valid type...
PG::Error: ERROR: type "hstore" does not exist is the exact error.
You must 'CREATE EXTENSION hstore' first
Hum ... Useful, I've made the same mistake ;-)
see :
- http://stackoverflow.com/questions/12540865/error-with-rails-test-database-using-postgres-using-hstore
- http://blog.remarkablelabs.com/2012/12/a-love-affair-with-postgresql-rails-4-countdown-to-2013
If none of the solutions doesn't work. You can do a 'fix' by adding
'CREATE EXTENSION hstore'
in schema.rb file before any table is created and after running migrations.
After that you can call rake db:test:prepare and it will work.
Later you can just discard changes from schema.rb file.
Thank you Ryan, I am very excited about Rails 4 and can't wait for it to hit at least pre release.
With all these changes under the hood do you expect it to be tough switching from Rails 3? Or the transitions this time should be smoother than from Rails 2.x to 3.x?
I've messed with both.
It should be a fairly simple transition, assuming you aren't using things that have been deprecated in Rails 3 (which would be removed in Rails 4)... and of course assuming you are at least on Rails 3.1. The Asset Pipeline is a fairly large hurdle.
It should be fairly simple. Most of the new stuff could be used in Rails 3.2 as gems.
I describe the process on this article: http://rubysource.com/get-your-app-ready-for-rails-4/
thanks Ryan!
i will learn ember.js :)
btw, anyone having this?
$ rake db:test:prepare
rake aborted!
PG::Error: ERROR: type "hstore" does not exist
LINE 1: ...te, "tags" character varying(255)[], "properties" hstore, "c...
config.active_record.schema_format = :sql
helped to run minitest with hstore
I get that from simply doing an rake db:migrate on development with an hstore type :(
I had the same problem and the solution is basically that you have to enable hstore on the database. Run the following (after you have created the db of course :)
For futher information and how to do this as a rails migration see this
Yeah, I've figured that out, but it would be nice to know why Ryan didn't have to do that. Is there a better way?
Ryan did that command in the migration file:
https://github.com/railscasts/400-what-s-new-in-rails-4/blob/master/blog-after/db/migrate/20130104011551_create_articles.rb#L3
also just do a migration for hstore:
def self.up
execute "CREATE EXTENSION hstore"
end
def self.down
execute "DROP EXTENSION hstore"
end
end
Amazing episode Ryan, as always !
And I love your last words about others skills than programming ! I personally want to make music in 2013 :)
If you are interested in listening to my music, check out https://soundcloud.com/thibaut-assus-1 :)
I would be also amazed that you could come Ryan to Paris.rb ! I have a couch and I can show you some amazing places in of Paris ! => http://meetup.rubyparis.org/
Happy new year, and stay how you are, amazing and so helpful !
For security concerns, is there any possible way to disable the route-showing feature and /rails/info feature?
Those will only work in development environment, so there's no need to worry about that in production.
Ryan uses the
rails
executable in the episode. It is important to mention that depending on your setup, that probably will call your 3.2.X install of rails. While Rails is in beta and there is no prerelease gem, you need to usebin/rails
:This bit me when I found that
rails s
did not start the server.If you're using the haml-rails gem, for the moment you may get an error running generators, something like:
This error is fixed by a recent pull request.
Update your Gemfile to use the master branch for the moment...
Congrats. 400 Episodes and still great.
I'm following along but getting some no
hstore
method erros when I run the migrationsrails g scaffold article name content:text published_on:date tags properties:hstore
and in the migration does this
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
However, when I ran this migration I got an undefined method 'hstore' error. I then went into my test database and created the hstore extension as you see below (rather than in the migration file as Ryan did)
psql rails4test2_development
psql (9.2.3)
Type "help" for help.
rails4test2_development=# CREATE EXTENSION hstore;
But I got the same error when I ran the migration.
undefined method
hstore' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition:0x007fa7ee7a9138>/Users/me/Sites/rails4test2/db/migrate/20130511204911_create_articles.rb:9:in
block in change'/Users/me/Sites/rails4test2/db/migrate/20130511204911_create_articles.rb:4:in `change'
Can anyone help me understand why this isn't working for me?
Hi Ryan! Thanks for the challenge! It sounds really cool :) +1
Ryan, you are an outstanding teacher. 1000+1 thanks for your railscasts.
Hi great tutorial .. but have a problem with checkboxes nothing append when i'am updating??
some body has the same probleme?
Not sure if you figured it out, but just ran into the same problem.
in the strong_params method, you must make tags an array
Hey
$ ruby -v
ruby 2.0.0p481
$ rails -v
Rails 4.1.5
I got some problem with
When I tryed to do a update, It leaved :tags out. It is becaus it expect a sting, not a array
This is the fix
Ryan, pretty nice screencast as always. However, the migration is incorrect. It is irreversible due to the execute statement present. So, it can not be 'rake db:rollback'ed !! Specific up / down methods could be used here instead.
Ryan,
It appears this screencast is no longer playing. In Chrome, it says "Can't play video. Media source loading has failed." It won't play in Safari, either, though no error is displayed.
I'm not having trouble with any of the other screencasts, just this one.
this video isn't working :(
same here, Ryan, do you think the video can be fixed somehow?
yeah its not working
Same here, not working for me either.
same here. tried to download in itunes and says: "This movie requires QuickTime, which is not supported by this version of iTunes."
You should be able to just include
gem 'haml-rails'
in your Gemfile now and have it work.http://stackoverflow.com/questions/18485147/haml-rails-on-rails-4-0
Excellent!
Running rake tasks:
bin/rake test:models
Running a Rails command:
bin/rails console