Rails 4 Cheat-sheet

0 Shares
0
0
0

image

I am listing different points finding while I am navigating in Rails 4.  However, I am new in rails 4 so I may keep editing as I continue learning about it:

  • Requires at least ruby 1.9.3
  • Support for native data types in Postgress (ex. array, hstore (for storying sets such as key,value pairs))
  • Only shows warning messages instead of all log messages when you migrate
  • Method “ .all” :  In rails 4 is an active record relation instead of returning an array as a result  (ex. Person.all.class => Active Record::ActiveRecord_Relation_Person).  So, now to get the results that we used to get in rails 3, we need to call it as Person.all.to_a
  • Scope method deprecated
  • New method “.load” : Will trigger the query in the database but return it in active record relation instead of an array
  • New method: “.none” : It will return an active record relations but will return non records and not hitting the database. This is useful if we dont want to never return records but allow other scopes to be called on it 
  • New method: “where.not” : you can call not in the where scope and it will invert it
  • “.order” now accepts hash: example => Person.order(name: :desc) so we can pass the column name and the order that we want to be sorted and we can pass multiple columns as well
  • New Method “.find_by”: you can pass a hash into this and it doesnt require a method missing to be implemented 
  • New Method “.find_or_create_by”:  also you can pass a hash and it works similar to the above .find_by method
  • New model call ActiveModel::Model :  This is a class that let us act like active record but not save it to the database and we just need to add include ActiveModel::Model and add attr_accessor, etc.  To implemented we just need to create a class and inside of the class, we add the “include ActiveModel…”
  • The Welcome Page: Similar to the rails 3 application but in rails 4, its dynamically generate it by rails internals so we dont have to remove it from the public folder
  • New gem ‘sprockets-rails’
  • New gen ‘turbolinks’: Just FYI we can remove it by remove it from the gemfile,  also delete it from the application.js file and from the application.html.erb file we need to remove where its mention it 
  • Cash digest is different from rails 3.2
  • Controller:  
  • Scaffolding loads a model record using the before filter which is now call before_action but it does the same thing 
  • Update action response to a put request or patch request which seems to fit the behaviour of update action or clear actionUpdate action response to a put request or patch request which seems to fit the behaviour of update action or clear action
  • The update attribute was rename to just “update” but we can still use update_attribute
  • Support force control parameters built on it  (strong parameters) so that in our Model we dont need the attr_accessible line 
  • Model:
  • New directory call “concerns” where we can use to add models to help reduce large models 
  • Views:
  • There are a lot of new helper methods that can be use to generate new form fields 
  • We can now have checkboxes and we add them using: <=%f.collection_check_boxes :tag, %w[option1, option2,etc], :to_s, :to_s%>
  • Helper methods for HTML5 input types for example the date_select field can be change to date_field and it can represent a text field for some browsers like safari  or other browsers like Chrome will handle it as a smart date selector
  • Support for a ruby template handler so instead of using a html.erb file we can directly use a .ruby extension (FYI, its “.ruby” instead of “.rb”).  So we can generate forms using straight ruby which is useful to handle jason formats 
  • Routes:
  • New way that help remove duplication on routes which is possible by adding:

“concern :<name of potential duplicate route> do

`                  resources: <name of potential duplicate route>

end

        and then we can just call it as resources :users, concern: :<name of potential duplicate route>

  • Match method in the routes is not longer supported (so now we need to specified what type of request we want to make ex. get, post, etc)
  • Turn to a default attributes when you generate a specific url by supplying a constraints to a route. Example:

       get ‘myroute”, to: ‘home#index’, contraints: {protocol: “http”, subdomain: “test”}

>> myapp.myroute_url

=>“http://test.example.com/myroute”

  • Config file:
  • Under application.rb file we can add console configuration by passing  the following code:

console do

   require ‘pry’

   config.console = Pry

end

  • on config/environments/production.rb, the trace.save was replace by eager_load because it suppose to be more save
  • Test Directory:
  • The organization of the files is different and now we can find a folder for testing controller, model files included on it so its more structure 
  • It uses minitest which we dont see it but the file test_helper.rb has the class ActiveSupport::TestCase which that inherits from minitest
  • Exception Page/Error/missing page:
  • This page has changed on appearance and content and now it shows us a table of the routes for the application 
  • Also we can get information about the routes available from the localhost url by going  the the localhost:3000/rails/info/routes
  • NEW:
  • Action controller live
  • Things Remove from Rails 3.2(which can be brought back by including gems):
  • Active Resource
  • Model Observers
  • Page and Action chaching 
  • Disable rake caching by default 
  • Resources about Rails 4:
  • http://blog.wyeworks.com/2012/11/
0 Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
RESTful routes Here is a good  cheatsheet to take for anyone who is a beginner and its trying…
Read More
San Francisco – Japanese Tea Garden (El Jardin Japonés) “No sé lo que se supone debo hacer, pero…
Read More