" Live as if you were to die tomorrow. Learn as if you were to live forever.. "
- Mahatma Gandhi

Uploading Image To Dropbox Using Paperclip-dropbox

Posted on November 23rd, 2015 by Hare Ram Rai

In this tip I am going to explain how to upload image to a dropbox account. For uploading image to dropbox we have a Ruby gem “paperclip-dropbox”. But before uploading image we have to create a dropbox application at https://www.dropbox.com/developers/apps You have to fill up a form to create an application , below is a […]

Creating an Event on Google Calendar using Ruby on Rails

Posted on November 23rd, 2015 by Hare Ram Rai

The option to share events on Google Calendar is an important feature introduced by Google to as an answer to the changing needs of users as they spend more time and socialize online. This tip demonstrates how to add an event to Google Calendar using Ruby on Rails. Before starting the application we need to […]

How to use Savon in Ruby on Rails Application

Posted on November 23rd, 2015 by Hare Ram Rai

In this tips I am going to introduce you to web service API call. I am using http://webservicex.net/ “WebserviceX.NET provides programmable business logic components and standing data that serve as “black boxes” to provide access to functionality and data via web services”. I am going to use one of its web service  which is “Currency […]

Integrating Simple_Form Gem To Rails Application

Posted on November 23rd, 2015 by Ajay Sahu

Why we have chosen simple_form : – simple_form is lightweight – It comes up with basic HTML mark up giving freedom to customize the CSS at your own comfort. Build a basic application: $ rails new DemoApp $ rails g scaffold blog title:string description:text $ rake db:migrate Now check app/blogs/_form.html.erb : <%= form_for(@blog) do |f| […]

Make current_user available in Model File

Posted on November 23rd, 2015 by AJAY SAHU

Let’s say we are adding a basic signing functionality to our application and we want the current_user(which is under User Controller)  should  be available for other Model file. We have a model called  Comment and we wish to make current_user accessible here. Inside app/views/controller/ApplicationController.rb define a method which assigns current_user to a cattr_accessible attribute defined […]

Add Pagination in Rails

Posted on November 23rd, 2015 by Ajay Sahu

This tip shows how to add pagination in Rails. Let’s create a basic rails project : $rails new pagination #creating project $cd pagination $rails generate scaffold user name:string address:string #Createed a basic app with 2 fields name & Address $rake db:migrate #let’s create our users table in Database. Now run your rails server & Navigate […]

Introduction To Rails Scaffolding

Posted on November 23rd, 2015 by AJAY SAHU

What ? In this tip I am going to demonstrate how to create your first basic Rails application without writing a single line of Ruby code. How ? Rails has a beautiful concept called scaffolding,using which a novice programmer can also create his first Rails App. Just follow 5 basic steps and get your first […]

Send SMS from fullonsms.com via our Rails Application

Posted on November 23rd, 2015 by AJAY SAHU

By using  this small Trick we can send SMS to any Indian Mobile number from fullonsms via our Rails Application. For this we need  a gem ‘mechanize’  It will help us to render our html pages, forms, form elements etc . So let’s go ahead and install this gem for our Rails application. Add gem […]

Render Prev & Next Record From Show Page in Rails

Posted on November 23rd, 2015 by AJAY SAHU

Scenario: In Rails we can click individual record and open it from index page but to open the next record we need to go back to index & click another link.  But we can have a Prev / Next Navigation options in our show page to render our previous/next records without clicking individual links. We […]

Run a Specific DB Migration in Rails

Posted on November 23rd, 2015 by AJAY SAHU

Scenario: Let’s say we are having 10 migration files in our application. Our DB is already set as per our migrations. Now let’s say we got a requirement to add a new column to an existing table. Literally we need to update our migration file and synchronize the same with database. Description: We are having […]

Clear Cache in Rails

Posted on November 23rd, 2015 by AJAY SAHU

On implementing basic sign in functionality we need to handle our session properly. Even after proper session management, in following case we can view users confidential profile information. ->login into app ->Move to profile page -> Tap on Logout ->Press back button of browser -> Getting my last visited page(profile Page) We need to clear […]

By Default Start Your Webrick Server On Different Port Number

Posted on November 23rd, 2015 by Ajay Sahu

Generally on starting the server, it takes port 3000. If you want to change port then we can specify port number manually by appending our rails s with -p port_no. But we can change our default port no by adding few lines of codes. With Following Command our  Rails Server starts at Default port 3000 […]

Speed up your Webrick Server

Posted on November 23rd, 2015 by Ajay Sahu

In development environment when you try to access your application via other system, app behaves quite slow. With a small configuration with our server we can make it faster. Speed up your Webrick Server : Locate your config.rb File: For Linux User: /usr/lib/ruby/1.9.1/webrick/config.rb For Windows User: C:\RailsInstaller\Ruby1.9.3\lib\ruby\1.9.1\webrick\config.rb Replace following line: :DoNotReverseLookup => nil, with :DoNotReverseLookup […]

How To Implement Filter On Controller Actions In Rails

Posted on November 23rd, 2015 by AJAY SAHU

What exactly filter is ? Filter is a simple method which is executed “before” / “after” / “around” a controller action. For example, one filter might check if the user has the credentials for accessing a particular action in a controller. Where can I place my  filter ? Filters are inherited so if you  a […]

Integrate Simple_form Gem To Rails Application

Posted on November 23rd, 2015 by AJAY SAHU

We got many gems to customize our form in rails out of which we will be using simple_form. Why we have chosen simple_form : – simple_form is lightweight.   – It comes up with basic HTML mark up giving freedom to customize the CSS at your own comfort. Build a basic application: $ rails new DemoApp […]

Keep Our Drop Down & Index List Sorted For Better User Experience

Posted on November 23rd, 2015 by AJAY SAHU

Generally when we fetch records from our DB and render those in our view without caring about sorting those records. Let’s consider a case, we have doctors name in a dropdown and patient needs to chose one doctor whose name is “James Brito”.  If we have 1000 doctor names then it’ll be quite tough for […]

How to create custom model validator in Rails

Posted on November 23rd, 2015 by Ajay Sahu

Rails ships with nice collection of model validations. By default rails facilitizes our application with validations like presence, uniquiness, format etc etc. In most applications default supplied validations are enough to serve our purpose. But in few cases we are required to add few custom validations to our rails applications. Problem statement : Let’s assume, […]

Detect & Customize Installed Gem Versions in Rails

Posted on November 23rd, 2015 by AJAY SAHU

Here we’ll see 3 basic rails commands to Identify which version of gems we are using. In many instances our application does not support couple of gem version. in that case we need to detect what’s the exact version, then need to remove the unsupported version to make our app run cleanly. Run below commands […]

Seed Your Database With Dummy Records In Rails

Posted on November 23rd, 2015 by AJAY SAHU

In data-drive development environment we need couple of pre-existing data in our application. Literally in your rails application if we wish to deploy few dummy / starting data then we can use seed and execute it in deployment script (everytime when a new app is installed). -We will place our dummy records in our db/seeds.rb […]

Save Model attributes from unauthorized access

Posted on November 23rd, 2015 by AJAY SAHU

Mass Attribute updates are a good time saving feature in rails. But in case if we want to protect your model attributes  and we don’t want users to be able to update it directly ???? In Such case rails Active Record provides two built-in ways to protect  attributes from mass-assignment. We have two options here […]

Skinny C & Fat M approach in Rails

Posted on November 23rd, 2015 by Ajay Sahu

Basically whe we are novice to rails application, we used to place all our codes in our view pages and used to keep our model & controller files almost blank. For a sample app  our view, controller and model files used to look some way similar to this : #view               Here we […]

Use of ‘prawn’ gem to generate PDF in Rails

Posted on November 23rd, 2015 by Shatabdi Mohapatra

Add this gem to the gemfile of your application and run the ‘bundle’ command. gem ‘prawn’ Create a PDF Mime::Type inside config/initializers/mime_types.rb. Mime::Type.register “application/pdf”, :pdf   I have created a respond block with HTML and PDF format inside my controller. Inside the format.pdf block, the send_data method is called. respond_to do |format| format.html format.pdf do […]

Downloading Attachments From Mails Through IMAP in Rails

Posted on November 23rd, 2015 by Shatabdi Mohapatra

Sometimes there is a requirement to download the attachments of mails being accessed thorugh IMAP protocol in Rails. 1. Firstly, we need to connect to imap and login to the mail account of the user. def get_messages require ‘net/imap’ require ‘mail’ imap = Net::IMAP.new(params[:host], params[:port], params[:ssl]) imap.login(params[:username], params[:password]) 2. Then we need to select the […]

Using Mongo_mapper for MongoDB

Posted on November 23rd, 2015 by Anuj Dubey

According to mongodb.org, in rails there are two leading gems Mongo_mapper and mongoid to use mongodb as database . Here we will see mongo mapper with rails. In rails Mongomapper is an easy way to model our application and persist our data in MongoDB. To install MongoMapper gem gem install mongo_mapper class Employee include MongoMapper::Document […]

Use MySQL DB for your rails Application ( Windows)

Posted on November 23rd, 2015 by Ajay Sahu

Here I am assuming ruby on rails setup + MySQL Database has been installed in your windows machine. We will follow couple of steps and will use MySQL Database for our rails application. Step -1: $ rails new mySQL_Example –database=mysql #by applying –database=mysql parameter, check your Gemfile, it must include gem ‘mysql2’. our procedure seems […]