Wednesday, 14 August 2013

dynamic programing in rails controller

Here is the simple code snippet for dynamic programming in rails controllers.

  def method_missing(c)
    render_error(404)
  end


The above method will be invoked when there was method missing exception was raised and we are redirected the request to specific page like 404.

Thank You,
Uma Mahesh

git commands

Here are the list of useful git commands.

> git config -l

List all variables set in config file.

> git config --global user.name "uma mahesh"
> git config --global user.email "umamaheshvarma@gmail.com"



Thank You,
Uma Mahesh.
 

Saturday, 13 July 2013

postgresql console interface


Here is the command to connect to postgresql through console,

> sudo -u postgres psql

The above command will connect to psql.

>  sudo -u postgres psql
psql (9.1.9)
Type "help" for help.

postgres=# 


Now you can run all the sql queries through this interface.



Thank You,
Uma Mahesh.




 

Wednesday, 10 July 2013

Hi,

While working with rspec I had issue as shown below.

undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_1

I didn't change any code modifications in the spces and my spces started failing with the error shown above. I have searched for the solution and I didn't found any specific solution for that. At last I came to find the issue is due to change in the Rspec version in my gem file.

I reverted back to my old gem file and finally able to solve the issue.

Thank You,
Uma Mahesh.

Sunday, 16 June 2013

couldn't find file 'twitter/bootstrap'

Twitter Bootstrap in rails application not working in production while deployed to heroku.

When I check for logs in heroku, I am able to see the below error.

ActionView::Template::Error (couldn't find file 'twitter/bootstrap')

While doing some google I came to see that, I have included  'twitter-bootstrap-rails' gem in development env in my gem file. So it was unable to load in production.

I have made the 'twitter-bootstrap-rails' gem public and bundle install again. Now every thing works fine.

Thank You,
Uma Mahesh.

sqlite3 error while pusing code to heroku


Below is the issue while I am trying to push the code to heroku for the first time.

rails@rails:/media/AAFA1CCBFA1C95A3/ruby/rails_app/simple_form/sample1$ git push heroku master
Counting objects: 99, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (88/88), done.
Writing objects: 100% (99/99), 39.73 KiB, done.
Total 99 (delta 3), reused 0 (delta 0)

-----> Ruby/Rails app detected
-----> Installing dependencies using Bundler version 1.3.2
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
       Fetching gem metadata from https://rubygems.org/.........
       Fetching gem metadata from https://rubygems.org/..
       Installing rake (10.0.4)
       Installing i18n (0.6.1)
       Installing multi_json (1.7.7)
       Installing activesupport (3.2.13)
       Installing builder (3.0.4)
       Installing activemodel (3.2.13)
       Installing erubis (2.7.0)
       Installing journey (1.0.4)
       Installing rack (1.4.5)
       Installing rack-cache (1.2)
       Installing rack-test (0.6.2)
       Installing hike (1.2.3)
       Installing tilt (1.4.1)
       Installing sprockets (2.2.2)
       Installing actionpack (3.2.13)
       Installing mime-types (1.23)
       Installing polyglot (0.3.3)
       Installing treetop (1.4.14)
       Installing mail (2.5.4)
       Installing actionmailer (3.2.13)
       Installing arel (3.0.2)
       Installing tzinfo (0.3.37)
       Installing activerecord (3.2.13)
       Installing activeresource (3.2.13)
       Installing rack-ssl (1.3.3)
       Installing json (1.8.0)
       Installing rdoc (3.12.2)
       Installing thor (0.18.1)
       Installing railties (3.2.13)
       Installing bootstrap-daterangepicker-rails (0.0.5)
       Installing coffee-script-source (1.6.2)
       Installing execjs (1.4.0)
       Installing coffee-script (2.2.0)
       Installing coffee-rails (3.2.2)
       Installing commonjs (0.2.6)
       Installing jquery-rails (3.0.1)
       Installing less (2.2.1)
       Installing less-rails (2.2.3)
       Installing libv8 (3.3.10.4)
       Using bundler (1.3.2)
       Installing rails (3.2.13)
       Installing sass (3.2.9)
       Installing sass-rails (3.2.6)
       Installing simple_form (2.1.0)
       Installing sqlite3 (1.3.7)
       Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
       /usr/local/bin/ruby extconf.rb
       checking for sqlite3.h... no
       sqlite3.h is missing. Try 'port install sqlite3 +universal'
       or 'yum install sqlite-devel' and check your shared library search path (the
       location where your sqlite3 shared library is located).
       *** extconf.rb failed ***
       Could not create Makefile due to some reason, probably lack of
       necessary libraries and/or headers.  Check the mkmf.log file for more
       details.  You may need configuration options.
       Provided configuration options:
       --with-opt-dir
       --without-opt-dir
       --with-opt-include
       --without-opt-include=${opt-dir}/include
       --with-opt-lib
       --without-opt-lib=${opt-dir}/lib
       --with-make-prog
       --without-make-prog
       --srcdir=.
       --curdir
       --ruby=/usr/local/bin/ruby
       --with-sqlite3-dir
       --without-sqlite3-dir
       --with-sqlite3-include
       --without-sqlite3-include=${sqlite3-dir}/include
       --with-sqlite3-lib
       --without-sqlite3-lib=${sqlite3-dir}/lib
       --enable-local
       --disable-local
       Gem files will remain installed in /tmp/build_27eau1y5d86jq/vendor/bundle/ruby/1.9.1/gems/sqlite3-1.3.7 for inspection.
       Results logged to /tmp/build_27eau1y5d86jq/vendor/bundle/ruby/1.9.1/gems/sqlite3-1.3.7/ext/sqlite3/gem_make.out
       An error occurred while installing sqlite3 (1.3.7), and Bundler cannot continue.
       Make sure that `gem install sqlite3 -v '1.3.7'` succeeds before bundling.
 !
 !     Failed to install gems via Bundler.
 !

 !     Push rejected, failed to compile Ruby/Rails app

To git@heroku.com:simple-form-app.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@heroku.com:simple-form-app.git'

 Solution: 

Add this to your Gemfile,
group :production do
  gem 'pg'
end
group :development, :test do
  gem 'sqlite3'
end
 
then do a bundle then repush to heroku. You cannot use sqlite3 on Heroku - which is the cause of the error.

Thank You,
Uma Mahesh.


Wednesday, 12 June 2013

helper method from rails console

Hi,

I would like to test the helper method from the rails console. Can any one suggest me how I can invoke a helper method from console?

Thank You,
Uma Mahesh.

Friday, 24 May 2013

how to run rails console in production environment


We run the below command for getting into rails console.

> rails c

By default it will load the development environment. But some time you may need to run in production env to check some configuration variables.

You can run the rails console by specific environment with the below command.

RAILS_ENV=production rails console

This will load the production environment setting to the console env.

Thank You,
Uma Mahesh.

Thursday, 16 May 2013

Synchronous and Asynchronous difference

Hi,

Many of the folks are quite confused about the Synchronous and Asynchronous request in web application. May of us know that AJAX is an asynchronous request. But what does asynchronous mean?

Asynchronous request will not wait for response for an request. Let me explain with an example. 

function Namecheck(){
var avilability = 1;
avilability = get_status("/user/check_name");
if avilability == 1
{
alert('avilable');
}
else{
alert('Not avilable');
}
}

In the above case 'get_status' will send an ajax request to "user/check_name" and it will not wait for the response for that request as it is an asynchronous, there by we always get the javascript alert as "avilable". So the function will execute without ant wait for response. This is how asynchronous request will work. 

Synchronous request is the one which will wait until the complete response was received. 

Note: to over come the response issue with AJAX we will use callback to trigger a function once the complete request was done with receiving a response. 


Thank You,
Uma Mahesh

Tuesday, 14 May 2013

Email domain validation in ruby

Hi,

Most of the web applications will have registration forms and every form will have email field as the mandatory. We use regular expression to validate the email format. But some times we may come across tio validate the given email id was valid or not that means does the domain exists or not.

Below is the useful code to validate the domain name in ruby with the help of MX records.


In email_validation.rb


require 'resolv'

class EmailValidation
def self.validate_email_domain(email)
domain = email.match(/\@(.+)/)[1]
Resolv::DNS.open do |dns|
@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
end
puts @mx.size > 0 ? true : false
end
end


s = EmailValidation.validate_email_domain("umamaheshvarma@gmail.com")


The above method will return true if the domain exists.


Thank You,
Uma Mahesh