Thursday 22 August 2013

Exception Notifier Plugin for Rails



ROR version

        Ruby=>3.2.13
        Rails=>1.9.3
   

Gemfile

    Add  below gems into your Gemfile

            gem "mail"
            gem "exception_notification",
                     :git => "git://github.com/rails/exception_notification.git",
                       :require => "exception_notifier"


if you going to run application in development mode 

                 development.rb

                        config.action_mailer.raise_delivery_errors = true(default false so you must chang true)

                    add below two lines

                         config.action_mailer.delivery_method = :smtp
                         config.action_mailer.perform_deliveries = true

                   application.rb(config/)

                               add below configuration into application.rb

                            config.action_mailer.smtp_settings = {
                                             :address              => "smtp.gmail.com",
                                             :port                 => 587,
                                             :domain               => 'test.com',
                                             :user_name            => 'notifier@gmail.com',
                                             :password             => 'xxxxxxx',
                                             :authentication       => :plain,
                                            :enable_starttls_auto => true  }
 

                         config.middleware.use ExceptionNotifier,
                                        :email_prefix => "[myAppliction Error Report]  ",
                                        :sender_address => %{"notifier" <notifier@gmail.com>},
                                        :exception_recipients => %w{gowthaman@gmail.com}
                           end
       
     if you going to run application in production mode 
    
                   production.rb

                    add below three lines

                         config.action_mailer.raise_delivery_errors = true
                         config.action_mailer.delivery_method = :smtp
                         config.action_mailer.perform_deliveries = true

                  and add below configuration into production.rb

                            config.action_mailer.smtp_settings = {
                                             :address              => "smtp.gmail.com",
                                             :port                 => 587,
                                             :domain               => 'test.com',
                                             :user_name            => 'notifier@gmail.com',
                                             :password             => 'xxxxxxx',
                                             :authentication       => :plain,
                                            :enable_starttls_auto => true  }
 

                         config.middleware.use ExceptionNotifier,
                                        :email_prefix => "[myAppliction Error Report]  ",
                                        :sender_address => %{"notifier" <notifier@gmail.com>},
                                        :exception_recipients => %w{gowthaman@gmail.com}
                           end



If you want to send notifications from a background process like DelayedJob, you should use the notify_exception method like this:

begin
  some code...
rescue => e
 ExceptionNotifier.notify_exception(e)
          end

Refer: gem exception_notification

Friday 24 May 2013

How to clone a single branch in git?


How to clone a single branch in git?
I have a local git repository called 'skeleton' that I use for storing project skeletons. It has a few branches, for different kinds of projects:
casey@agave [~/Projects/skeleton] git branch
* master
  rails
  c
  c++



git clone user@git-server:project_name.git -b branch_name /some/folder

pdfkit command failed: “/usr/bin/wkhtmltopdf” “--page-size” “Letter” “… ”--quiet“ ”-“ ”-"

Error:

  Create PDF Error :
 
  RuntimeError (command failed: "/home/ubuntu/.rvm/gems/ruby-1.9.3-p392@xxx/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-right" "0.75in" "--margin-bottom" "0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--quiet" "-" "-")

Solution:


 (1)  first, installing dependencies
 $sudo aptitude install openssl build-essential xorg libssl-dev

(2) for 64bits OS Run one by one following commands.

$sudo wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
$sudo tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
$sudo mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
$sudo chmod +x /usr/local/bin/wkhtmltopdf

Finally Go to your rails app/config/initializer Folder and create new file pdfkit.rb and paste following code in it.

PDFKit.configure do |config|
   config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf' if Rails.env.production?
end


 I refer generating-pdfs-in-rails-with-pdfkit-and-deploying-to-a-server link

Monday 20 May 2013

How to resolve jcode (LoadError) with contacts gem in rails 3? change line 21 to:




Ruby >= 1.9 doesn't have jcode, a module to handle japanese (EUC/SJIS) strings, as it supports unicode natively.

So you will need to add: require 'jcode' if RUBY_VERSION < '1.9' to your gdata gem found under your .rvm directory somewhere similar to this:

/home/.rvm/gems/ruby-1.9.2-p0@your_gemset_name/gems/gdata-1.1.1/lib/gdata.rb
change line 21 to:

if RUBY_VERSION < '1.9'  require 'jcode'  $KCODE = 'UTF8'end

Sunday 5 May 2013

How to clone a single branch in git?branch_name



I have a local git repository. has a few branches, for different kinds of projects:

open terminal

   # cd  /home/project path
         
                git branch
             * master
                rails
                dev
 


Solution:

           git clone user@git-server:project_name.git  -b branch_name      /some/folder

Monday 29 April 2013

how to run a scheduler.rake file in rails?



If you don't see your command in the response list then it is not well formed or just does not have description. So you need to investigate your rake file. Rake tasks files have similar structure, like:

namespace :some_task do

  task :do_some_work => :environment do
    # Code goes here
  end

  desc 'Do some other work'
  task :do_something => :environment do
    # Code goes here
  end

end

#first task
rake some_task:do_some_work

#second task
rake some_task:do_something

Sometimes there could be no namespace and then tasks could be executed just using rake command and name of the task as first argument.

Tuesday 23 April 2013

ruby installation is missing psych (for yaml output).



Problem it seems your ruby installation is missing psyh (for yaml output).centos solution curl -O http://www6.atomicorp.com/channels/atomic/centos/6/x86_64/RPMS/atomic-release-1.0-14.el6.art.noarch.rpm sudo rpm -Uvh atomic-release-1.0-14.el6.art.noarch.rpm sudo yum install libyaml-devel rvm reinstall 1.9.3-p194



Friday 29 March 2013

gem MySQL installation



Gem:gem mysql install 

Well,we have the solution for you. Your Linux installation requires that the development libraries for MySQL to be installed in order to build the gem.  but sometimes   attempting to install the MySQL gem and you are receiving this error?

ERROR:
Fetching: mysql-2.9.1.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** 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/bin/ruby1.8
--with-mysql-config
--without-mysql-config
--with-mysql-dir
--without-mysql-dir
--with-mysql-include
--without-mysql-include=${mysql-dir}/include
--with-mysql-lib
--without-mysql-lib=${mysql-dir}/lib
--with-mysqlclientlib
--without-mysqlclientlib
--with-mlib
--without-mlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-zlib
--without-zlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-socketlib
-- .......................
       --.........................

Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/mysql-2.9.1 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/mysql-2.9.1/ext/mysql_api/gem_make.out

Here is the proper way for install this gem:

SOLUTION:

               sudo apt-get install libmysql-ruby
                      sudo apt-get install libmysqlclient-dev
                      sudo gem install mysql

Friday 22 March 2013

Ruby on Rails image upload




In this blog we see how to add image  in our rails application            
       
                     rails g uploader image( image replace any name)

  Gem file add following gem  
           GemFile

                                    gem "rmagick"
                                    gem "carrierwave"


in your model for example your user model


      model/user.rb
 
                  class user < ActiveRecord::Base
                     attr_accessible : :name, :user_image  
                     mount_uploader :user_image, ImageUploader
                  end

and

app/uploaders/image_uploader.rb


          class ImageUploader < CarrierWave::Uploader::Base
                        include CarrierWave::RMagick
                       storage :file             
                      def store_dir                      
                          "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"     
                       end           
                     version :thumb do                   
                          process :resize_to_limit => [200, 200]                   
                     end        
          end

Show.html.erb

        <%= image_tag painting.user_image_url(:thumb) if painting.image? %>

insert.htm.erb

              <%= f.file_field :user_image %>





Saturday 2 March 2013

Installing rmagick

Error

   Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /home/gowthaman/.rvm/rubies/ruby-1.9.2-p320/bin/ruby extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for gcc... yes
checking for Magick-config... no
Can't install RMagick 2.13.2. Can't find Magick-config in /home/gowthaman/.rvm/gems/ruby-1.9.2-p320/bin:/home/gowthaman/.rvm/gems/ruby-1.9.2-p320@global/bin:/home/gowthaman/.rvm/rubies/ruby-1.9.2-p320/bin:/home/gowthaman/.rvm/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/share/ruby-rvm/bin

*** 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
--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=/home/gowthaman/.rvm/rubies/ruby-1.9.2-p320/bin/ruby


Gem files will remain installed in /home/gowthaman/.rvm/gems/ruby-1.9.2-p320/gems/rmagick-2.13.2 for inspection.
Results logged to /home/gowthaman/.rvm/gems/ruby-1.9.2-p320/gems/rmagick-2.13.2/ext/RMagick/gem_make.out
An error occurred while installing rmagick (2.13.2), and Bundler cannot continue.
Make sure that `gem install rmagick -v '2.13.2'` succeeds before bundling.


Solution:

         sudo apt-get install libmagickcore-dev libmagickwand-dev

git Installation

  Step 1 : sudo apt-get install git-core

  Step 2 : git config --global user.name "your user name"

  Step 3 : git config --global user.email "your email id"

  Step 4 : ssh-keygen -t rsa -C "Enter your mail id"

                  Generating public/private rsa key pair.

 Step 5 : Enter file in which to save the key (/home/gowthaman/.ssh/id_rsa): ssh-add (type this)
                        Note:It will suggest a default file name id_rsa.pub.
 Step 6 : Enter passphrase (empty for no passphrase):

 Step 7 : Enter same passphrase again:

                    Your identification has been saved in ssh-add.
                    Your public key has been saved in ssh-add.pub.
                    The key fingerprint is:80:15:61:4f:99:c9:e9:99:f4:d1:20:3e:8c:25:ad:27 your mail
                    The key's randomart image is:
                                   +--[ RSA 2048]----+
                                    |      ..+oo+o+   |
                                    |       + B*o+ .  |
                                    |      + .+=o .   |
                                    |     . .E =..    |
                                    |        So .     |
                                    |                 |
                                    |                 |
                                    +-----------------+

  Step 8 :Copy public key from ssh-add.pub. That key file contain somw key with your email.

            Key Like "ssh-rsa           AAAAB3NzaC1yc2EAAAADAQABAAABAQDLO1ARmpfDQl8CpdEPB9AVonxp9q05opFfi+Ix/Urn64Dyc2xhLyQMRVx8fd7XyA1mH4rAwVAyKhEWHJfo9QJsShDtS23q/Xq3QDQVwMrLDVMuFlTsKFdXL6zOe08W4D5kKJoj3fjKWyXdbwh9OHj1P1IdqxfwNTpnzRMCFHHLzIACIALBVyI2sdj7FPy2LktLmnXl3VXu9N6HKb3oWAXly+NDHmAaX6hYx81TjrOMNXwWqHpK5SYl5hX7k6KKbBlaJqeuPZ07tyK9/Q7FVbFiSue/3FN5LVllTST4M7f5ax+XBsKnRre+18mApB51Wy6mIzdq++/7X9LAXZZjSzl7"(Remove your email id and copy key).

Step9 : open your git account and edit profile add ssh keys.

Step 10:  Finally after add ssh key and verify   key fingerprint  in your mail.
  
                    git clone  help
            git clone  git@github.com:gowthaman14/jQuery-Validation-Engine.git

     ERROR

                Cloning into 'jQuery-Validation-Engine'...
                Permission denied (publickey).
               fatal: The remote end hung up unexpectedly

   Check that you are connecting to the correct server

    ssh -vT git@github.com

   you get
Permission denied (publickey).

   ssh -T gowthaman14@github.com(your git account name@github.com)

     you get
Permission denied (publickey).

Make sure you have a key and SSH is using it


To verify that you have a key generated and loaded into SSH, run ssh-add -l:

 ssh-add -l

print--like this  2048 a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users/you/.ssh/id_rsa (RSA)

If it does not print out anything, you need to add your key to SSH by running

-->ssh-add path/to/key.(ssh-add  ......setpath ssh-add.pub)




  

Wednesday 20 February 2013

Querys in Ruby on Rails

Query's in Ruby 

  In this post we discuss about how to retrieve  data from database like mysql or sql ...

     Select
 
        Model.where("column = ? , value)
        Model.where("column = ? or other_column = ?", value, other_value)
        Model.where("column = ? and other_column = ?", value, other_value)

Sunday 17 February 2013

Rails Time/Date Formats (strftime)


Rails Time/Date Formats (strftime)

Method:

               <%= @contact.created_at.strftime("%d/%m/%Y")  %>


 Year
%Y     year with century 2007
%y     year without century 07
%C     century number (year divided by 100) 20
Month
%B     full month name January
%b     abbreviated month name Jan
%h     same as %b Jan
%m     month as number (01-12)
Week
%U     week number of the year, Sunday as first day of week (00-53)
%W     week number of the year, Monday as first day of week (00-53)
Day
%A     full weekday name Wednesday
%a     abbreviated weekday name Wed
%d     day of the month (01-31)
%e     day of the month, single digits preceded by space ( 1-31)
%j     day of the year (001-366)
%w     weekday as a number, with 0 representing Sunday (0-6)
%u     weekday as a number, with 1 representing Monday (1-7)
Time
%H    hour (24-hour clock) (00-23)
%k     hour (24-hour clock); single digits preceded by space ( 0-23)
%I     hour (12-hour clock) (01-12)
%l     hour (12-hour clock); single digits preceded by space ( 1-12)
%M     minute (00-59)
%S     seconds (00-59)
%p     either AM or PM AM
%Z     timezone name or abbreviation EDT
%z     timezone offset from UTC -0400



Thursday 14 February 2013

Ruby installation and create application

Ruby on Rails

Installation:

  • Rails-3.2.11
  • Ruby 1.9.2

 Ruby Installation:

      •   curl -L https://get.rvm.io | bash -s stable
      • --bash -sk stable < <(curl -sk                  https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
      • [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
      • rvm install 1.9.2(if you need 1.9.3  you change 1.9.3)

Rail instalation:


                            gem install rails r=3.2.11

Create New Project:

           step 1 -->[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

           step 2 -->rvm --create use1.9.2@name(gemset give any name)

           step 3--> rails new blog(projectname)

   step 4--> rm -rf Gemfile.lock

          step 5->change gem 'mysql2' in GemFile
           step 6-->bundle install(used for maintain rail version)
-->Exception
An error occurred while installing mysql2 (0.3.11), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.11'` succeeds before bundling.
-->Solution
       sudo apt-get install libmysqlclient-dev

          step 7--> rake db:create
Exception:
                      Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
solution
-->gem install execjs

        step 8--> rails g scaffold Question(give your application name any name) title:string             
                      questions:string answers:string(field name)
                                                                       rails      mysql or other
:binary     blob
         :boolean    tinyint(1)
:date     date
:datetime   datetime
:decimal    decimal
:float   float
:integer    int(11)
:string     varchar(255)
:text     text
:time     time
:timestamp  datetime


step 9-->-->rake db:migrate

step10-->rails server

       step 11:open browser type url localhost:3000/ blog (projectname)

   if  you add model or controller to your application follow below instruction 

      model
                 rails g model User(modelname)
      controller
                 rails g controller User (controller name)
      mailer
                rails g mailer  user_mail(mailer name)
      migration 
             scaffold
               rails g scaffold Question(give your application name any name) title:string          
                      questions:string answers:string(field name)
       add new-column
                    rails g migration add_fieldname_to_tablename  fieldname:string