Category: Ruby on Rails

Multiple Database connections in rails

By , July 29, 2011 2:20 pm

Here I am going to show how to connect  multiple database connection in rails.

Lets say there are 2 models

1. Company:  in db1

2. Website: in db2

First set connection parameters  for both database with different constant variables and define in environment file.


DB1 = {

:adapter => 'mysql',

:database => DATABASE1,

:username => USERNAME,

:password => PASSWORD,

:host => HOST

}

DB2 = {

:adapter => 'mysql',

:database => DATABASE2,

:username => USERNAME,

:password => PASSWORD,

:host => HOST

}

Add connection.rb file in lib folder


module Connection

  def self.included(base)

    base.class_eval do

    parameters = self::DB

    ActiveRecord::Base.establish_connection(

      :adapter  => parameters[:adapter],

      :host     => parameters[:host],

      :username => parameters[:username],

      :password => parameters[:password],

      :database => parameters[:database]

      )

   end
 end
end

include connection file and set DATABASE in models

in Company model

DB =  DB1
include Connection

in Website model


DB = DB2

include Connection

Now whenever Company model is used, it will establish connection for DB1 and for Website model it will establish connection DB2.

Google Maps in rails 3

By , March 16, 2011 3:08 pm

I already wrote a blog on how to use Google maps in rails.  Earlier blog is supported in rails 2.3.x version.

To supoort rails 3 application , certain changes are required in plugin.

Here I will show you what are those changes required.

Download YM4R plugin :


rails plugin install https://github.com/rorcraft/ym4r_gm

1. Add following method in class Varible under gm_plugin/mapping.rb


def to_str
@variable + “;”
end

2. Use raw method for each google maps methods to avoid html contents.


<%= raw GMap.header %>

<%= raw @map.to_html %>
<%= raw  @map.div(:width => 600, :height => 400) %>

4. Add Ym4r js in your layouts

 <%=javascript_include_tag :defaults,"ym4r-gm"%>

5. Commented line no: 35 in ym4r/lib/gm_plugin/map.rb

#   a << "<script src=\"/public/javascripts/ym4r-gm.js\" type=\"text/javascript\"></script>\n" unless options[:without_js]

I hope this blog helps you. Let me know if you still facing any problem.

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.

Panorama Theme by Themocracy