Ruby On Rails

Fetching XML in rails (using REXML)

require ‘net/http’ require ‘rexml/document’ url = “http://maps.google.com/maps/api/directions/xml?key=*************************&sensor=false&origin=Chennai&destination=Trichy&#8221; # get the XML data as a string xml_data = Net::HTTP.get_response(URI.parse(url)).body # extract event information doc = REXML::Document.new(xml_data) duration = [] way = [] kms=[] full_text=[] doc.elements.each(‘DirectionsResponse/route/leg/step/html_instructions’) do |a| way<<a.text end doc.elements.each(‘DirectionsResponse/route/leg/step/distance/text’) do |a| kms<<a.text end doc.elements.each(‘DirectionsResponse/route/leg/step/duration/text’) do |a| duration<<a.text end way.each_with_index do|text, index| full_text<<“#{kms[index]}(#{duration[index]})—>#{way[index]}<br/>” end @full_text=full_text

Ruby On Rails

Google Maps in Rails

The Google Maps API  offers the ability to add mapping functionality, similar to maps.google.com, to their websites. All of Google Maps functionalities, including markers, directions, zoom options and satellite view are only a few lines of code away. To get a Google Maps API key, login to your google account (gmail.com), visit http://code.google.com/apis/maps/signup.html, type the… Continue reading Google Maps in Rails

JavaScript · Rails 3 · Ruby On Rails

Comparition of Adopting Unobstructive Javascript and XSS in Rails 2 & 3

Cross-Site-Scripting in Rails 2 <%= @post.body %> –> Unsafe <%= h @post.body %> –> Safe Cross-Site-Scripting in Rails 3 <%= @post.body %> –> Safe <%= raw @post.body %> –> Unsafe Adopting Unobstructive Javascript Example 1 : Rails 2: <%= link_to_remote ‘Show’, :url => post %> Will generate a HTML like, “<a href=”#” onclick=”new Ajax.Request(‘/posts/1′, {asynchronous:true,… Continue reading Comparition of Adopting Unobstructive Javascript and XSS in Rails 2 & 3

Rails 3

Difference in ActionMailer between rails 2 and rails 3

Rails 2 def welcome(user)     @recipients = “#{user.email}”     @from = “admin@samplesite.com”     @subject = “Welcome Mail”     @body[:user] = user end Rails 3 def welcome(user)     @user = user     mail(:from=>”admin@samplesite.com”, :to=>”#{user.email}”, :subject=>”Welcome Mail”) end We can also set the default value if it is common for all… Continue reading Difference in ActionMailer between rails 2 and rails 3

MySQL · Ruby On Rails

DB and Log backup’s using Rake Tasks

Add this in your rakefile for taking log and db backup’s. require ‘fileutils’ namespace :backup do   #for taking log backup -start   desc ‘backup log’   namespace :log do    desc ‘rake backup:log:all’    task :all do     backup_path = File.join(Rails.root, ‘backup’, ‘log’, “#{Date.today.month}”)     FileUtils.mkdir_p(backup_path) unless File.exist?(backup_path)     filename = File.join(backup_path, “log_#{Time.now.strftime(“%Y%m%d”)}.tar.gz”)    … Continue reading DB and Log backup’s using Rake Tasks