Active admin resolve kaminari and will paginate gem conflict issue

Add config/initializers/kaminari.rb

Kaminari.configure do |config|
config.page_method_name = :per_page_kaminari

Add config/initializers/will_paginate.rb

if defined?(WillPaginate)
ActiveSupport.on_load :active_record do
module WillPaginate
module ActiveRecord
module RelationMethods
def per(value = nil) per_page(value) end
def total_count() count end
module CollectionMethods
alias_method :num_pages, :total_pages

Pagination example on active admin

controller do
def index
index! do |format|[:page])


Bootstrap – Images

<!DOCTYPE html>
<html lang=”en”>
<title>Bootstrap Example</title>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<link rel=”stylesheet” href=””&gt;
<script src=””></script&gt;
<script src=””></script&gt;

<div class=”container”>
<h2>Rounded Corners</h2>
<p>The .img-rounded class adds rounded corners to an image (not available in IE8):</p>
<img src=”cinqueterre.jpg” class=”img-rounded” alt=”Cinque Terre” width=”304″ height=”236″>

<div class=”container”>
<p>The .img-circle class shapes the image to a circle (not available in IE8):</p>
<img src=”cinqueterre.jpg” class=”img-circle” alt=”Cinque Terre” width=”304″ height=”236″>

<div class=”container”>
<p>The .img-thumbnail class creates a thumbnail of the image:</p>
<img src=”cinqueterre.jpg” class=”img-thumbnail” alt=”Cinque Terre” width=”304″ height=”236″>
<div class=”container”>
<p>The .img-responsive class makes the image scale nicely to the parent element (resize the browser window to see the effect):</p>
<img src=”cinqueterre.jpg” class=”img-responsive” alt=”Cinque Terre” width=”304″ height=”236″>
<div class=”container”>
<h2>Responsive Embed</h2>
<div class=”embed-responsive embed-responsive-16by9″>
< iframe class=”embed-responsive-item” src=”  “> </iframe>



Angular JS implementation with Rails 4

Step 1 : Create rails app using rails new employee_details
Step 2 : Remove turbolinks gem from gemfile and add ‘pg’ gem for postgres. Set the rails version to 4.1.2 (Turbolinks may not work with angular js)
Step 3 : Run bundle install
Step 4 : Change database.yml to use postgres
adapter: postgresql
encoding: unicode
database: employee_details
username: postgres
password: root
pool: 100
Step 5 : Create the model
rails g model EmployeeDetail employee_id:integer employee_name:string expertise:string experience:integer education:string mobile:string address:text gender:boolean
Step 6 : Run rake db:create and rake db:migrate
Step 7 : Create a controller
rails g controller employee_details
class EmployeeDetailsController < ApplicationController
def index
Step 8 : Create a view file for employee_details
<h1>Ruby on Rails Rocks</h1>
Step 9 : Add routes for index page
root ’employee_details#index’
Step 10 : Change layout file, remove turbolinks, add ng-app with the app name and point angular javascripts
<!DOCTYPE html>
<html ng-app=”EmployeeDetails”>
<title>Employee Details</title>
<%= stylesheet_link_tag ‘application’, media: ‘all’ %>
<%= csrf_meta_tags %>

<%= yield %>
<script src=”//”></script>
<script src=”//”></script>
<%= javascript_include_tag “application” %>

Step 11 : change assets/javascripts/application.js
//= require app
//= require_tree ./../angular
Step 12 : Define app which we referred above in
window.App = angular.module(‘EmployeeDetails’, [‘ngResource’])
Step 13 : Define custom routes
EmployeeDetails::Application.routes.draw do
root ’employee_details#index’
Step 14 : Create directories as below
Running the app now should display ‘Ruby on Rails Rocks’
Step 15 : Change the index.html to below
<div ng-controller=”EmployeeDetailsCtrl”>
<h1>Message: {{title}}</h1>
Step 16 : Now create the angular controller (inside assets/angular/controllers/ referred above and assign title
App.controller ‘EmployeeDetailsCtrl’, [‘$scope’, ($scope) ->
$scope.title = “Ruby on Rails Always Rocks!”
Restart the server. Running the app now should display ‘Ruby on Rails Always Rocks’
Step 17 : Change the index method of controller and render json
class EmployeeDetailsController < ApplicationController
def index
render json: EmployeeDetail.all
Step 18 : Generate few records using seed
#Emp ID, Emp Name, Expertise, Experience, Education, Mobile, Address, Gender
employees = EmployeeDetail.all.count
if employees == 0
employee_id = [101,102,103,104,105]
employee_name = [“Arun”, “Kumar”, “Raja”, “Karthik”, “Surya”]
expertise = [“RubyOnRails”, “English Literature”, “PhP”, “Admin”, “Java”]
experience = [5,4,3,4,5]
education = [“M.C.A”, “M.A”, “B.E”, “B.E”, “B.E”]
mobile = [“9987656478”, “9878965437”,”9786587987″, “9879547897”, “9876096543”]
address = [“Chennai”, “Madurai”, “US”, “Cuddalore”, “Trivandrum”]
gender = [0,1,1,0,0]
for i in 0..4
employee_id: employee_id[i], employee_name: employee_name[i],
expertise: expertise[i], experience: experience[i],
education: education[i], mobile: mobile[i],
address: address[i], gender: gender[i]
Step 19 : Run rake db:seed
Now go and run the app, you should see the records in JSON format.
Step 20 : Create the home controller (we will be using this controller for angular implementation)
class HomeController < ApplicationController
def index
Step 21 : Apply root to home controller index method and create api routes for angular
scope :api do
get “/employee_details(.:format)” => “employee_details#index”
get “/employee_details/:id(.:format)” => “employee_details#show”
root ‘home#index’
Step 22 : Get the records on angular controller
App.controller ‘EmployeeDetailsCtrl’, [‘$scope’, ‘EmployeeDetail’, ($scope, EmployeeDetail) ->
$scope.showEmployeeDetail = null
$scope.selectedRow = null

$scope.employee_details = EmployeeDetail.query ->
$scope.showEmployeeDetail = $scope.employee_details[0]
$scope.selectedRow = 0

$scope.showEmployeeDetail = (employee, row) ->
$scope.showEmployeeDetail = employee
$scope.selectedRow = row
Step 23 : Create the service for api
App.factory ‘EmployeeDetail’, [‘$resource’, ($resource) ->
$resource ‘/api/employee_details/:id’, id: ‘@id’

Step 24 : Now change the view file for listing employees (app/views/home/index.html.erb)
<div ng-controller=”EmployeeDetailsCtrl”>
<li ng-repeat=”employee in employee_details”>
<h3>{{employee.employee_id}} <small>({{employee.employee_name}})</small></h3>
<div ng-show=”showEmployeeDetail”>
Expertise : {{showEmployeeDetail.expertise}}


Restart the server. Executing the application at http://localhost:3000 will now display the list of employees and the information of first employee.

Naming conventions on Ruby & Ruby on Rails

Ruby Naming Conventions

Local Variables
Lowercase letter followed by other characters, naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. mileage, variable_xyz

Instance Variables
Instance variables are defined using the single “at” sign (@) followed by a name. It is suggested that a lowercase letter should be used after the @, e.g. @colour 

Instance Methods
Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters, e.g. paint, close_the_door

Class Variables
Class variable names start with a double “at” sign (@@) and may be followed by digits, underscores, and letters, e.g. @@colour

Constant names start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT

Class and Module 
Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase

Global Variables
Starts with a dollar ($) sign followed by other characters, e.g. $global

Rails Naming Conventions

Variables are named where all letters are lowercase and words are separated by underscores, e.g. order_amount, total

Class and Module 
Classes and modules use MixedCase and have no underscores, each word starts with a uppercase letter, e.g. InvoiceItem

Database Table
Table names have all lowercase letters and underscores between words, also all table names need to be plural, e.g. invoice_items, orders

The model is named using the class naming convention of unbroken MixedCase and is always the singular of the table name, e.g. table name might be orders, the model name would be Order. Rails will then look for the class definition in a file called order.rb in the /app/models directory. If the model class name has multiple capitalised words, the table name is assumed to have underscores between these words.

Controller class names are pluralized, such that OrdersController would be the controller class for the orders table.  Rails will then look for the class definition in a file called orders_controller.rb in the /app/controllers directory.

Files, Directories and other pluralization
Files are named using lowercase and underscores. Assuming we have an Orders controller then the following other conventions will apply:

  • That there is a helper module named OrdersHelper in the orders_helper.rb found in the app/helpers directory
  • Rails will look for view template files for the controller in the app/views/orders directory
  • Output from this view will then be used in the layout defined in the orders.html.erb in the app/views/layouts directory
  • Test files including order_test.rb will be created in the /test/unit directory, a file will be created in the /test/fixtures directory called orders.yml and finally a file called orders_controller_test.rb will be created in the /test/functional directory

Primary Key
The primary key of a table is assumed to be named id.

Foreign Key
The foreign key is named with the singular version of the target table name with _id appended to it, e.g. order_id in the items table where we have items linked to the orders table.

Many to Many Link Tables
Tables used to join two tables in a many to many relationship is named using the table names they link, with the table names in alphabetical order, for example items_orders.

Automated Record Timestamps
You can get ActiveRecord to automatically update the create and update times of records in a database table. To do this create two specially named columns created_at and updated_at to your table, i.e. t.datetime :created_at and t.datetime :updated_at. If you only want to store the date rather than a date and time, use :created_on and :updated_on.

Naming Convention Summary

Model Naming Convention

Table: orders
Class: Order
File: /app/models/order.rb
Primary Key: id
Foreign Key: customer_id
Link Tables: items_orders

Controller Naming Convention

Class: OrdersController
File: /app/controllers/orders_controller.rb
Layout: /app/layouts/orders.html.erb

View Naming Convention

Helper: /app/helpers/orders_helper.rb
Helper Module: OrdersHelper
Views: /app/views/orders/… (list.html.erb for example)

Tests Naming Convention

Unit: /test/unit/order_test.rb
Functional: /test/functional/orders_controller_test.rb
Fixtures: /test/fixtures/orders.yml




Requirement:  There are two web applications AppX,AppY. All user relevant informations are stored under AppX. When user signing in into AppX, that time user session would be available in AppX. Because devise authentication mechanism is applied only on AppX. Also the same user session has to be applied for AppY,while user trying to access AppY. It should not ask user credentials again.

Let us see how to  implement single sign on using active resource in a rails application. Here is a basic pictorial representation of single sign on concept.


Concept of this picture is,

  • Sharing the sessions between two rails applications (AppX, AppY)
  • Assigning authentication token in session.
  • Assigning authentication token into request header while making resource call from AppY to AppX, as session is not available through resource call.
  • When user trying to access AppY directly, its would redirect to login page of AppX  since user session is not…

View original post 831 more words

ArgumentError: invalid byte sequence in UTF-8

You might be receiving ‘ArgumentError: invalid byte sequence in UTF-8‘ error if the URL consists of invalid byte sequences like http://localhost:3000/users/signup?%28t%B3odei%29

To get rid of this error, just add the below to your Gemfile

gem 'utf8-cleaner'

and make bundle install.

utf8-cleaner gem removes invalid bytes on the URL and save your app from the page crash……