Image preview by javascript

By , October 13, 2011 5:32 pm

Here I  will  show how to display image preview before uploading to server side.

#HTML


<!DOCTYPE html>

<html>

<head>

<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<meta charset=utf-8 />

<title>JS Bin</title>

<!--[if IE]>

<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

<![endif]-->

<style>

article, aside, figure, footer, header, hgroup,

menu, nav, section { display: block; }

</style>

</head>

<body>

<input type='file' onchange="showImagePreview(this);" />

<img id="image_preview" src="#" alt="your image" />

</body>

</html>

#Javascript


function showImagePreview(input) {

if (input.files && input.files[0]) {

var reader = new FileReader();

reader.onload = function (e) {

$('#image_preview

.attr('src', e.target.result)

.width(150)

.height(200);

};

reader.readAsDataURL(input.files[0]);

}

}

Note: This method is not worked in IE as files property of file field is not supported by IE browser

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Webnews
  • Digg
  • del.icio.us
  • Reddit
  • Bloglines
  • LinkedIn
  • YahooMyWeb
  • Facebook
  • Google Bookmarks
  • Mixx
  • MySpace
  • Technorati
  • TwitThis

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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Webnews
  • Digg
  • del.icio.us
  • Reddit
  • Bloglines
  • LinkedIn
  • YahooMyWeb
  • Facebook
  • Google Bookmarks
  • Mixx
  • MySpace
  • Technorati
  • TwitThis

Array.indexOf in Internet Explorer

By , July 22, 2011 2:32 pm

In javascript, to find value in array I am using indexOf method. This method works well in all browsers except IE.

IE throws error that “indexOf method is not defined”. So I found solution that added following code in javascript.


if(!Array.indexOf){

Array.prototype.indexOf = function(obj){

for(var i=0; i<this.length; i++){

if(this[i]==obj){

return i;

}

}

return -1;

}

}

So if browser does not find indexOf method then indexOf method is defined by above code. So this will helps to solve IE issues.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Webnews
  • Digg
  • del.icio.us
  • Reddit
  • Bloglines
  • LinkedIn
  • YahooMyWeb
  • Facebook
  • Google Bookmarks
  • Mixx
  • MySpace
  • Technorati
  • TwitThis

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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Webnews
  • Digg
  • del.icio.us
  • Reddit
  • Bloglines
  • LinkedIn
  • YahooMyWeb
  • Facebook
  • Google Bookmarks
  • Mixx
  • MySpace
  • Technorati
  • TwitThis

Send email from Ruby

By , February 8, 2011 5:05 pm

Here I will show you how to send email through ruby script.

Require NET/SMPT  library  in ruby script


require 'net/smtp'

Config SMTP authentication


def  send_email(to,opts={})

opts[:server]      ||= 'SERVER IP'

opts[:port]        ||= PORT_NUMBER

opts[:domain]      ||= "DOMAIN IP"

opts[:user_name]   ||="USERNAME"

opts[:password]    ||= "PASSWORD"

opts[:authentication]   ||="plain"

opts[:from]        ||= 'example@domain.com'

opts[:from_alias]  ||= 'FROM NAME'

opts[:subject]     ||= "SUBJECT SAMPLE"

opts[:body]        ||= "MESSAGE BODY"

 msg = <<END_OF_MESSAGE

From: #{opts[:from_alias]} <#{opts[:from]}>

To: <#{to}>

Subject: #{opts[:subject]}

Content-type: text/html

#{opts[:body]}

END_OF_MESSAGE
 Net::SMTP.start(opts[:server],opts[:port], opts[:domain],opts[:user_name], opts[:password],opts[:authentication]) do |smtp|

smtp.send_message msg, opts[:from], to

end

end

Now send email by passing arguments.


send_email "EMAIL ADDRESS", :body => "MESSAGE"

I hope this will helps you to send email through script.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Webnews
  • Digg
  • del.icio.us
  • Reddit
  • Bloglines
  • LinkedIn
  • YahooMyWeb
  • Facebook
  • Google Bookmarks
  • Mixx
  • MySpace
  • Technorati
  • TwitThis

Panorama Theme by Themocracy