Thursday, May 14, 2015

Renamed Callbacks in Rails 4

Action callbacks in controllers have been renamed from *_filter to *_action.
For example:

class UsersController < ApplicationController
  before_action :set_user, :except => [:index, :new, :create}
  before_action :require_the_president, :only => [:fire_the_missiles]
 
  private
 
  def set_user
    @user = somehow_find_and_set_the_user(params[:id])
  end
 
  def require_the_president
    @user.is_the_president?
  end
end 

The old *_filter callbacks still work and are not deprecated; so, you can still use them if you wish.

Streaming, via ActionController::Live

The new ActionController::Live module provides the ability to stream data to clients. Simply include the module into a controller to enable your app to send arbitrary streamed data. You'll have to use a threaded server, like thin and puma, in order to stream data; actions from streaming controllers run in a separate thread.

Here's an example from the Rails 4 documentation:

class MyController < ActionController::Base
  include ActionController::Live
 
  def stream
    response.headers['Content-Type'] = 'text/event-stream'
    100.times {
      response.stream.write "hello world\n"
        sleep 1
    }
    response.stream.close
  end
end

As the docs note, there are three things to keep in mind:

  • You must write any headers before you call write or close on the response stream.
  • You have to call close on the response stream when you're finished writing data.
  • Ensure that your actions are thread-safe, as they will run in a separate thread.

Routing Concerns

Routing Concerns is an attempt to DRY up your config/routes.rb. The basic idea is to define common sub-resources (like comments) as concerns and include them in other resources/routes. Here's the obvious example:

concern :commentable do
  resources :comments
end

concern :remarkable do
  resources :remarks
end

resources :posts, :concerns => :commentable 
resources :articles, :concerns => [:commentable, :remarkable] # can include several

The above is equivalent to the following Rails 3 code:

resources :posts do
  resources :comments
end

resources :articles do
  resources :comments
  resources :remarks
end

Personally, I'm not sure this adds much value; perhaps it makes sense for large applications with hundreds of routes.

Friday, April 17, 2015

Current request url with host name in rails

# request.env["REQUEST_URI"]
It return request url only without parameter.

http://www.google.com

# request.env["HTTP_REFERER"]

It return url with parameter.

http://www.google.com?q=text

Tuesday, March 31, 2015

Difference between nil and false in ruby

The differences of the methods nil and false are:

- nil cannot be a value, where as a false can be a value
- A method returns true or false in case of a predicate, other wise nil is returned.
- false is a boolean data type, where as nil is not.
- nil is an object for NilClass, where as false is an object of for FalseClass

In sort::

False is a boolean datatype 
Nil is not a data type 

Wednesday, March 25, 2015

Difference between "include" and "extend" in Ruby?

extend: 
If you're using a module, that means you're bringing all the methods into your class. If you extend a class with a module, that means you're "bringing in" the module's methods as class methods.

include:
If you include a class with a module, that means you're "bringing in" the module's methods as instance methods.

EX:


 module A
   def say
     puts "this is module A"
   end
 end

 class B
   include A
 end

 class C
   extend A
 end

Output:

 B.say
 => undefined method 'say' for B:Class



 B.new.say
 => this is module A



 C.say
 => this is module A



 C.new.say
 => undefined method 'say' for C:Class

What is the use of 'require' in ruby ?

Please run below commands on rails console.

Input:     require "file_name"
Output:  true => load the file

Input:    require "file_name"
Output: false => already load

Input:    require "file_name"
Output: Error => Need to add this file

FastAPI: The Modern Python Web Framework

  Introduction to FastAPI: The Modern Python Web Framework The world of web frameworks has always been competitive — from Django and Flask ...