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
development:
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
end
end
Step 8 : Create a view file for employee_details
<div>
<h1>Ruby on Rails Rocks</h1>
</div>
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”>
<head>
<title>Employee Details</title>
<%= stylesheet_link_tag ‘application’, media: ‘all’ %>
<%= csrf_meta_tags %>
</head>
<body>
<header>
Angular
</header>

<%= yield %>
<script src=”//ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js”></script>
<script src=”//ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular-resource.min.js”></script>
<%= javascript_include_tag “application” %>

</body>
</html>
Step 11 : change assets/javascripts/application.js
//
//= require app
//= require_tree ./../angular
Step 12 : Define app which we referred above in app.js.coffee
window.App = angular.module(‘EmployeeDetails’, [‘ngResource’])
Step 13 : Define custom routes
EmployeeDetails::Application.routes.draw do
root ’employee_details#index’
end
Step 14 : Create directories as below
assets/angular
assets/angular/controllers
assets/angular/services
assets/angular/directives
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>
</div>
Step 16 : Now create the angular controller (inside assets/angular/controllers/employeedetailsCtrl.js.coffee) 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
end
end
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
EmployeeDetail.create(
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]
)
end
end
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
end
end
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”
end
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”>
<ul>
<h1>{{title}}</h1>
<li ng-repeat=”employee in employee_details”>
<h3>{{employee.employee_id}} <small>({{employee.employee_name}})</small></h3>
</li>
</ul>
<div ng-show=”showEmployeeDetail”>
<h2>{{showEmployeeDetail.employee_id}}</h2>
<p>{{showEmployeeDetail.employee_name}}</p>
<p>
Expertise : {{showEmployeeDetail.expertise}}
</p>
</div>
</div>

 

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 
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

Variable 
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

Model 
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
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

SINGLE SIGN ON USING ACTIVE RESOURCE

SINGLE SIGN ON USING ACTIVE RESOURCE – Just Re-blogging

RamOnRails

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.

sso_1

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

Useful tools for video/audio/screen recorder

Below are few useful tools that you can use for audio/video recording, Screen capturing etc.., Especially these will be very much useful for recording the content of your screen with audio to produce recorded format of DEMO. Recommended for use in Windows.

Best:-

http://camstudio.org/

Other options:-

http://www.totalrecorder.com/

http://www.techsmith.com/

http://www.screencast-o-matic.com/

Useful tools for diagrams

Below are few useful tools that can make use of it to draw UML diagrams, data flow diagrams, ER diagrams etc..,

Best:-

https://www.lucidchart.com

Other options:-

http://www.gliffy.com/uses/uml-software/

http://creately.com/Draw-UML-and-Class-Diagrams-Online

http://yuml.me/

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……