The knowledge sharing platform BitsNTricks brings another piece of information for you. Below are some new features added in Ruby On Rails 4.
1. No more vendor/plugins: The vendor/plugins directory has been removed in Rails 4. Now, you can use Bundler with a path or Git dependencies.
2. Many deprecated items are moved into separate gems: Rails 4 has deprecated many things and has moved them into separate gems.
3. Live Streaming: Before Rails 4, streaming was limited to templates only, but now its real time streaming. It is now supported via Rails. HTTP streaming is the process of doling out the response to a request over a period of time, rather than all at once. It’s often used for media (such as streaming video) or to provide a real-time output of another program. While streaming, the Rails application can produce output, wait for new data to be generated or just wait for some time and continue streaming some more output.
To use streaming, simply include the ActionController::Live module in your controller and use response.stream.write to write text or data to the stream. When you’ are finished working on this, use response.stream.close. This is a small addition, but it opens up all kinds of things as far as live media is concerned.
- We have to write headers before writing or closing the call on response stream.
- When we have finished writing the data, then we have to close the call.
- Rails 4 is thread-safe, so your actions must be thread-safe. Note: They are running in separate thread.
4. Hash-based & dynamic finder methods: Before Rails 4, hashes and finder method was used. In rails 4 these have been removed to the deprecated finders gem, which by default is included in Rails 4 as dependency. Now we can use method find_by_* type methods.
5. Default Headers: Rails 4 is rich in Default Headers. In this the developer is able to include an option where he can set headers that are returned with each response from the application. On the other hand, browser supports these headers and provides a secure experience to the users
In Rails 4, the following headers will be set by default on each response:
config.action_dispatch.default_headers = { 'X-Frame-Options' => 'SAMEORIGIN', 'X-XSS-Protection' => '1; mode=block', 'X-Content-Type-Options' => 'nosniff' }
This powerful feature is also useful for mobile and desktop application, where we are able to include some form of metadata by default in all request. We can add headers in controller as well.
6. Mass Assignment Protection in the Controller: Rails 4 will be done by essentially white listing or blacklisting attributes for an object inside the controller. Imagine that we have the following user class in our application:
# Assume the following fields: [:id, :first, :last, :email]
class User < ActiveRecord::Base
end
Mass assignment allows us to set a bunch of attributes at once:
attrs = {:first => "John", :last => "Doe", :email => "john.doe@example.com"} user = User.new(attrs) user.first #=> "John" user.last #=> "Doe" user.email #=> "john.doe@example.com"
7. Application Message Versifier: Rails 4.1 also included a built-in helper to generate signed messages with HMAC. The message verifier was previously used to power things like signed cookies, but it is now much easier to use it for other purposes. For example, you can implement a stateless “reset password” feature without storing any tokens in the database
8. Action Pack Variants: It’s a known fact that with passing time the preferences and needs change. Keeping the tradition alive, today, everyone wants a responsive design for a website. Sometimes, we need to tailor our views to serve the relevant content and workflow for the devices. It allows you to create different views for different device categories – such as mobile phones, tablets and desktops.
With Action Pack Variants, this will become much easier in Rails 4.1:
before_action :detect_device_variant
9. Array, MACADDR, INET, CIDR: Rails 4 support Array,MACADDR,INET & CIDR datatypes on PostgreSQL. These are generally stored as string in most databases. INET & CIDR are converted into instances of IPAddress class
10. Turbolinks: In Rails 4, a brand new piece of Javascript is going to be included that is Turbolinks. It is simliar to pjax. When Turbolinks are enabled in the website, it will fetch the page and replace the content with current page. The main difference between pjax and turbolinks is that pjax replace specific part of the page and turbolinks replace the whole page. It is included in rails as a Gem for faster navigation in the browser.
11. Russian Dolls: Russian Doll caching is a technique of nested fragment caching that auto expires fragments when the object time stamps change. It’s usage ensures that cache can be reused. Whenever the changes occurs in webpage the top most fragment cache expires.
class Team < ActiveRecord::Base has_many :members end class Member e ActiveRecord::Base belongs_to learn, aouc => true end <% cache @team do %> <h1><%. @team.name %></h1> <ul class="members"> <%= render @team.members 46> </ul> <% end %> And in app/views/rnembers/member.html.erb <% cache member do %> <li class="member> <%= member.name %> <span class="bio"><%= member.bio %></span> </li> <% end %>
If we had a team with two members, a total of of 3 fragment caches would be written:
views/members/1-20121220141922 views/members/2-20121220141922 views/teams/2-20121220141922
The above technique will work seamlessly until you have to modify the template of one of the fragments. Since the template is not taken into account in the fragment cache key, any changes to the template will not expire the cache. This quickly progresses in prefixing the fragment cache keys with a version, so that the expiration is be forced. A change in a nested fragment version will result in all parent versions needing a version bump also.
12. HTTP Patch Verb: In Rails 4, Patch verb is used for an update when resource is declared in routes file. The PATCH method identifies the set of changes described in the request entity, which can be applied to the resource identified by the Request URI. PUT and Update are still in use but if you want to use form_for to update, then you have to use PATCH method
13. Error handling in transaction callbacks: Before Rails 4, errors raised only on after_rollback or after_commit callbacks and only prints them to the logs. But in new version of rails, when you define a after_rollback or after_commit callback, you will receive a deprecation warning about this upcoming change.
14. Threadsafe by Default: Before Rails 4, config.threadsafe! could be removed, but for Rails 4.0 it is enabled by default. A big change to Rails 4 is that production applications will now be thread safe by default. This will have huge performance benefits for applications as they will be able to serve more than one request at a time.
Let’s take a look at the threadsafe! Method:
def threadsafe! @preload_frameworks = true @cache_classes = true @dependency_loading = false @allow_concurrency = true self end