Exception Notification in Rails 3

By , January 5, 2011 12:40 pm

Here I will show that how to configure exception notification in rails 3.

#Gem File


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

#Create exception_notification.rb in config/initializers folder and add following

YourAppName::Application.config.middleware.use ExceptionNotifier,

:email_prefix => "[ERROR] ",

:sender_address => '"Notifier" <notifier@yourdomain.com>',

:exception_recipients => ['exceptions@yourdomain.com']

Now recipients will get mail whenever exception raise in application.

Breadcrumbs in Rails 3

By , January 3, 2011 10:37 pm

Breadcrumbs on rails is very useful plugin for navigation feature in rails application.  Now here I will show that how breadcrumbs on rails works in rails 3 application. There are certain changes required in breadcrumbs on rails code to support rails 3 application.

  • Download Plugin.

rails plugin install git://github.com/weppos/breadcrumbs_on_rails.git
  • Add following code in application.rb

include BreadcrumbsOnRails::ControllerMixin
  • Changes in lib/breadcrumbs.rb.  Replace  following code in render_element method line  no:102

def render_element(element)

content = context.link_to_unless_current(compute_name(element), compute_path(element))

if @options[:tag]

@context.content_tag(@options[:tag], content.html_safe)

else

content.html_safe

end

end
  • Changes in lib/controller_mixin.rb.  Replace  following code in render_breadcrumbs method line  no: 93

def render_breadcrumbs(options = {}, &block)

builder = (options.delete(:builder) || Breadcrumbs::SimpleBuilder).new(self, breadcrumbs, options)

content = builder.render

if block_given?

concat(capture(content, &block)).html_safe

else

content.html_safe

end

end
  • Add Breadcrumbs in Controller:

class MyController

add_breadcrumb "home", :root_path

add_breadcrumb "my", :my_path

def index

# ...

add_breadcrumb "index", index_path

end

end
  • Add breadcrumbs in View

<body>

<%= render_breadcrumbs %>

</body>

Now Breadcrumbs will work in rails 3 application. For more breadcrumbs display options, You can visit on git

Simple Captcha in rails 3

By , December 10, 2010 2:41 am

Here I will show you how to add captcha in rails 3 application:

Download Simple captcha plugin from the git : click here


rails plugin install https://github.com/galetahub/simple-captcha.git

Generate Simple Captcha

rails generate simple_captcha
rake db:migrate

Generate scaffold of MODEL


rails g scaffold Model

Captcha code can be added two way . 1. Controller based 2. Model based

Controller Based

Include simple captcha in app/controllers/application.rb

ApplicationController < ActionController::Base
    include SimpleCaptcha::ControllerHelpers
end

Add captcha code in form page

<%=show_simple_captcha%>

Check captcha authentication in controller

def create
  if simple_captcha_valid?
    do something....
  else
    do something....
  end
end

Model Based
Add captcha in model

class Model < ActiveRecord::Base
   apply_simple_captcha
end

In Form page

<%= show_simple_captcha( :label => "human authentication",:object => "object") %>

In controller

 @object.valid_with_captcha?
or
@object.save_with_captcha

ADD I18n


simple_captcha:

  message:

    default: "Secret Code did not match with the Image"

    user: "The secret Image and code were different"

If application is in ruby 1.9.* then you need to require some changes in plugins otherwise you will not able to see captcha image and get following error

"TypeError (can't convert nil into Integer):"

Changes in /simple-captcha/lib/simple_captcha/image.rb
At line number 47: Comment class Tempfile

#class Tempfile < ::Tempfile

# Replaces Tempfile's +make_tmpname+ with one that honors file extensions.

# def make_tmpname(basename, n = 0)

# puts "**********************Base name: #{basename}"

# extension = File.extname(basename)

# puts "**********************Extension: #{extension}"

# puts "********************** SPRINFG #{File.basename(basename, extension)},#{$$},#{n.to_i}#{extension}"

#

# # sprintf("%s,%d,%d%s", File.basename(basename, extension), $$, n, extension)

# sprintf("%s%d-%d%s", File.basename(basename, extension), $$, n, extension)

# #"%s,%d,%d%s", File.basename(basename, extension), $$, n, extension

# puts "**********************Base name: #{basename}"

# end

# end


At line number 68: replace with below code

dst = Tempfile.new(['simple_captcha','.jpg'])

Hope this helps to solve above problem in rails 3.

No Such File to load — readline (LoadError) in ruby script/console

By , December 7, 2010 4:01 am

If you get following error while extecuting  ruby script/console


/usr/local/lib/ruby/1.8/irb/completion.rb:10:in `require’: no such file to load — readline (LoadError)
 from /usr/local/lib/ruby/1.8/irb/completion.rb:10
 from /usr/local/lib/ruby/1.8/irb/init.rb:252:in `require’
 from /usr/local/lib/ruby/1.8/irb/init.rb:252:in `load_modules’
 from /usr/local/lib/ruby/1.8/irb/init.rb:250:in `each’
 from /usr/local/lib/ruby/1.8/irb/init.rb:250:in `load_modules’
 from /usr/local/lib/ruby/1.8/irb/init.rb:21:in `setup’
 from /usr/local/lib/ruby/1.8/irb.rb:54:in `start’
 from /usr/local/bin/irb:13

To solve this issue , you need to install some libraries.


sudo apt-get install libncurses5-dev
sudo apt-get install libreadline5-dev

Then goto ruby source folder and ext/readline folder


cd  <RUBY SOURCE FOLDER>/ext/readline
ruby extconf.rb
make
sudo make install

This will solved your readline error. If you still facing any problem let me know.

Useful Date methods

By , November 12, 2010 4:53 am

Here I list down some of useful date methods. Generally following functions are used in Reports where reports required between specific period of time.

For example “This Month” data report  required starting date and ending date of month.

So by following way, both dates can get.


time = Time.now.to_date

Fri, 12 Nov 2010

from = time.beginning_of_month

Mon, 01 Nov 2010

to = time.end_of_month

Tue, 30 Nov 2010

For  ”This Year”

from = time.beginning_of_year

Fri, 01 Jan 2010

to = time.end_of_year

Fri, 31 Dec 2010

For “Today”

time.end_of_year

Fri, 31 Dec 2010

time = Time.now

Fri Nov 12 17:05:08 +0530 2010

time.beginning_of_day

Fri Nov 12 00:00:00 +0530 2010

time.end_of_day

Fri Nov 12 23:59:59 +0530 2010

Panorama Theme by Themocracy