November 19, 2015

Types of Caching in Rails

A few types of caching to know :
  • Page Caching: Save entire pages of an application without hitting the stack by returning cached pre-rendered pages. Good in applications without authentication and other highly dynamic aspects.
  • Fragment Caching: Allows fragments of view logic, for example partials or other bits of HTML that are independent from other parts, to be wrapped in a cache block and served out of the cache store when the next request comes in.
  • Action Caching: Works like Page Caching, except the incoming web request hits the Rails stack. This means that before filters (like authentication or other restrictions) can be run on it before the cache is served.
  • Rails.cache: All cached content except cached pages are stored in the Rails.cache.
  • HTTP Caching: HTTP caching occurs when the browser stores local copies of web resources for faster retrieval the next time the resource is required. It uses HTTP headers to determine if the browser can use a locally stored version of the response or if it needs to request a fresh copy from the server.

September 15, 2015

Uploading Multiple Images using CarrierWave

How to upload multiple image by selecting "ctrl + selction key" at same time.

This gem provides a simple and extremely flexible way to upload files from Ruby applications.
This gem has convenient support for multiple file upload fields.

Following are the steps:
For Complete Source code click here

In Rails, add it to your Gemfile:

gem 'carrierwave'
bundle install
rails generate uploader photo

Create post scaffold

rails generate scaffold post title:string

Create post_attachment scaffold

post_attachment post_id:integer photo:string
rake db:migrate

In post.rb

class Post < ActiveRecord::Base
  has_many :post_attachments
  accepts_nested_attributes_for :post_attachments

In post_attachment.rb

class PostAttachment < ActiveRecord::Base
   mount_uploader :photo, PhotoUploader
   belongs_to :post

In post_controller.rb

def show
  @post_attachments = @post.post_attachments.all

 def new
   @post =
   @post_attachment =
 def create
   @post =
   respond_to do |format|
       params[:post_attachments]['photo'].each do |a|
         @post_attachment = @post.post_attachments.create!(:photo => a)
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
      format.html { render action: 'new' }
  def post_params
    params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :photo])

In views/posts/_form.html.haml

 = form_for(@post, :html => { :multipart => true }) do |f|
    = f.label :title
    = f.text_field :title
  = f.fields_for :post_attachments do |p|
      = p.label :photo
      = p.file_field :photo, :multiple => true, name: "post_attachments[photo][]"
    = f.submit

In views/posts/show.html.haml

%p#notice= notice
  %strong Title:
  = @post.title
- @post_attachments.each do |p|
  = image_tag p.photo_url
  = link_to "Edit Attachment", edit_post_attachment_path(p)
= link_to 'Edit', edit_post_path(@post)
\#{link_to 'Back', posts_path}
Update form to edit an attachment views/post_attachments/_form.html.haml

= image_tag
= form_for(@post_attachment) do |f|
    = f.label :photo
    = f.file_field :photo
    = f.submit
 Now, update method in post_attachment_controller.rb

 def update
    respond_to do |format|
      if @post_attachment.update(post_attachment_params)
        format.html { redirect_to, notice: 'Post attachment was successfully updated.' }

For Complete Source code click here

June 4, 2014

Apple launches new Swift programming language for MAC OS X and iOS

Apple developers now have a new tool in their toolbox.

Switft, a new programming language that can power all of its devices. Swift is the successor to Objective-C, the venerable language that Apple has used to build apps for the Mac and iOS. Swift includes full support for Cocoa and Cocoa Touch, so developers can build apps for the iPad and iPhone.

Swift uses the same LLVM compiler that Apple uses for Objective-C, so developers can run Swift, Objective-C,  and C code all in the same program, and brings a number of improvements.

Swift seems to get rid of Objective C's reliance on defined pointers; instead, the compiler infers the variable type, just as many scripting languages do. At the same time, it provides modern features similar to those found in C++ and Java, like well-defined namespaces, generics, and operator overloading. From the few fragments of code shown during the demo, Swift appears to rely heavily on the dot-notation that Apple introduced in an earlier iteration of Objective C.

Apple showed off a couple of cases where implementing the same algorithm in Swift provided a speedup of about 1.3X compared to the same code implemented in Objective C. It also showed off a Swift "playground," where code is compiled as it's typed and the output is displayed in a separate pane of the editing window. The goal here is to allow developers to test code fragments without having to recompile an entire complex project.

The Swift Programming Language, This book is available for download with iBooks on your Mac or iOS device, and with iTunes on your computer. Books can be read with iBooks on your Mac or iOS device. Download link

May 15, 2014

What happens when you run Rails migration?

Rails migration allows the creation of the table and provides the functionality that can be performed on a table with the following commands:

create_table(name, options)
rename_table(old_name, new_name)
add_column(table_name, column_name, type, options)
rename_column(table_name, column_name, new_column_name)
change_column(table_name, column_name, type, options)
remove_column(table_name, column_name)
add_index(table_name, column_name, index_type)
remove_index(table_name, column_name)

-Rails Migration also allows the use of pre-defined data type in the application as it supports all the data types. The data types consist of string, integer, float, etc.

-Rails Migration allows the users to use the valid column options like limit (:limit=> "50"), default (:default => "hello"), and null (:null => false implies NOT NULL)

February 5, 2014

What is the difference between Symbol and String in Ruby?

The symbol in Ruby on rails act the same way as the string but the difference is in their behaviors that are opposite to each other.

The difference remains in the object_id, memory and process time for both of them when used together at one time.

Strings are considered as mutable objects. Whereas, symbols, belongs to the category of immutable.

Strings objects are mutable so that it takes only the assignments to change the object information. Whereas, information of, immutable objects gets overwritten.

String objects are written like
irb(main):001:0> "string object apple".object_id #=>70278982314860
irb(main):002:0> "string object
apple".to_sym.object_id #=> 456148

Symbols are used to show the values for the actions like equality or non-equality to test the symbols faster then the string values.

January 14, 2014

Difference between Argument and Parameter in ruby on rails?

A parameter represents a value that the method expects you to pass when you call it.
An argument represents the value you pass to a method parameter when you call the method. The calling code supplies the arguments when it calls the method.

In simple words, parameters appear in method definitions; arguments appear in method calls.

For example, in below method, variables param1 and param2 are the parameters

def foo_method(param1, param2):

while calling the method, arg1 and arg2 are the arguments
foo_method(arg1, arg2)

January 9, 2014

What are filters? and how many types of filters are there in ruby?

Filters are methods that are run before, after or "around" a controller action.

Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application.

Filter can take one of three forms: method reference (symbol), external class, or inline method (proc).



This content is not yet available over encrypted connections.