Rails : Mass assignment with ids in seeds.rb |PG::UniqueViolation: ERROR

Looks like you can mass-assign records into ActiveRecord models without problem like

                    { parent_id: 1, name: 'Computer Science', description: 'Readable via Electronic devices.' },
                    { parent_id: 1, name: 'Electronics' },
                    { parent_id: 1, name: 'Law' },
                    { parent_id: 1, name: 'Social' }

But you cannot do like Continue reading


Getting started with React.Js using NPM

My assumptions: You have prior experience of Web framework like 
  Ruby on Rails, Django, Phoenix, etc. 
Please refer to this React.js style guide for community comprehensible source code.

Hey, its not mandatory to use NPM to build applications with ReactJs; but, it will be alot easier to build, maintain, share codes using NPM. Firstly you should have NodeJs installed in your development machine.

Installation for Development Environment

$ sudo apt-get install nodejs
# try 
$ node -v
# v4.2.6
# if its says command not found: node then try 
$ nodejs
# If it shows up, it means due no some name conflicts 
# the name/command node is already taken by some binary. 
# So, its get it back.
$ sudo update-alternatives --install /usr/bin/node node /usr/bin/nodejs 10

Probable Issues: NPM not found

Continue reading


Rails|Ransack|Geocoder – Sort data by distance

If your application connects local businesses to local customer, then you might have used Ransack for sorting and searching of records. For geocoding and reverse-geocoding, you might be using Geocoder gem. If you want to let users to sort results by distance then its pretty easy.

Things to remember,

  • If you use Model.near([lat, long]) method, Ransack adds a new column called distance to the ActiverecordRelation object. You can access the method distance by prying.

Continue reading


SOAP with Ruby

SOAP (Simple Object Access Protocol) is a protocol specification for exchanging structured information in the implementation of web services in computer networks. Its purpose is to induce extensibility, neutrality and independence. It uses XML Information Set for its message format, and relies on application layer protocols, most often Hypertext Transfer Protocol (HTTP) or Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission. – Wikipedia

How to make SOAP calls in ruby

One of the best ways to make SOAP calls will be using the library Savon. Its very easy to get started.

If your service provider has WSDL (Web Services Description Language) document. You can also host the WSDL file in your local machine or deploy along with the source code. Here is sample WSDL file if you wanna have a look. Basically this document consists of all the operations supported, format of the response, the endpoint URL of the services and other details.  Continue reading


Rails : i18n : unexpected redirections

In my app, I have different User types like VehicleOwner , GarageOwner, Admin and unidentified/unauthenticated. I wanted to implement localization for English and Finnish. I was successful, but suddenly all my old URLs stopped working; when I tried to access the old URLs I was redirected to login page, and the login page was also not loading – stuck on infinite redirection loop.

# Initial implementation
unauthenticated :user do
  get '/:locale' => 'pages#home'
  root 'pages#home', page: :home

authenticate :user, ->(user) { user.vehicle_owner? } do
  get '/:locale' => 'vehicle_owners/quote_requests#index'
  root 'vehicle_owners/quote_requests#index'

authenticate :user, ->(user) { user.garage_owner? } do
  get '/:locale' => 'garage_owners/quote_requests#index'
  root 'garage_owners/quote_requests#index'


unauthenticated :user do
  get '/:locale' => 'pages#home', locale: /en|fi/
  root 'pages#home', page: :home

authenticate :user, ->(user) { user.vehicle_owner? } do
  get '/:locale' => 'vehicle_owners/quote_requests#index', locale: /en|fi/ # because should be addressed
  root 'vehicle_owners/quote_requests#index'

authenticate :user, ->(user) { user.garage_owner? } do
  get '/:locale' => 'garage_owners/quote_requests#index', locale: /en|fi/
  root 'garage_owners/quote_requests#index'

Rails | Devise | Internationalization I18n

If you are upgrading your app from uni-lingual to bilingual, you might want to have separate pages for Devise related pages as well. Its simple for simpler implementation of devise like no ‘omniauth’. But if you are including ‘omniauth’ then normal recommended approach will not work.

# Before
devise_for :users, path: '',
  path_names: { sign_up: :signup, sign_in: :login, sign_out: :logout },
  controllers: {
    registrations:      'devise_override/registrations',
    confirmations:      'devise_override/confirmations',
    sessions:           'devise_override/sessions',
    omniauth_callbacks: 'vehicle_owners/omniauth_callbacks'
# After
devise_for :users, only: :omniauth_callbacks, controllers: {omniauth_callbacks: 'vehicle_owners/omniauth_callbacks'}
scope "(:locale)", locale: /en|fi/ do
  devise_for :users, path: '',
             path_names:   { sign_up: :signup, sign_in: :login, sign_out: :logout },
             controllers:  {
                 registrations:      'devise_override/registrations',
                 confirmations:      'devise_override/confirmations',
                 sessions:           'devise_override/sessions'
             }, skip: :omniauth_callbacks

So, it turns out that you cannot scope the Omniauth related URLs, as it makes no sense. You might also see warnings like

Devise does not support scoping OmniAuth callbacks under a dynamic segment
and you have set "/(:locale)/". You can work around by passing
`skip: :omniauth_callbacks` to the `devise_for` call and extract omniauth
options to another `devise_for` call outside the scope. Here is an example:

 devise_for :users, only: :omniauth_callbacks, controllers: {omniauth_callbacks: 'users/omniauth_callbacks'}

 scope '/(:locale)', locale: /ru|en/ do
   devise_for :users, skip: :omniauth_callbacks

Note: Do not forget to restart the application after any major changes.