Posts

Showing posts with the label srikanth jeeva

HTML stripping, Truncating string in Ruby, Rails

For Truncating String I have used 'truncate' rails helper method. limiting to 10 characters truncate("im srikanth glad to meet you", 10) >> "im srik..." For HTML stripping, I have used gsub.. str.gsub(/ ]*>/, "")

Weekday or not..

You can Use this , def weekday? (1..5).include?(wday) end check .. d = Date.today => Mon, 04 Oct 2010 d.weekday? => true d = Date.today - 1 => Sun, 03 Oct 2010 d.weekday? => false

Sending HTML mail in rails

Hi pals, If all the mails that you send are in HTML format. its simple. Specify this line in environment.rb ActionMailer::Base.default_content_type = "text/html" If only one action has to be in HTML mail, specify 'content_type' in that action. content_type "text/html" example, def signup_notification(recipient) recipients recipient.email_address_with_name subject "New account information" from "system@example.com" content_type "text/html" end cheers, Sri

Making a http/https post request

Spent an hour to find out how to send a https post request and this is how .. require 'rubygems' require 'net/http' require 'net/https' require 'uri' http = Net::HTTP.new('facebook.com', 443) http.use_ssl = true path = "/oauth/access_token" data = 'id=123456' resp, data = http.post(path, data) puts resp.inspect puts data.inspect

Rails - select class not applied

Hi, previously my code was like this, I tried much using, html_options, options, but class is not applied for select. <%= f.select 'difficulty', options_for_select({ "Easy" => "1", "Medium" => "3", "Hard" => "5"}, get_difficulty(@tour).to_s), :class =>'input_text' %> Right way to do: <%= select :tour, :difficulty, { "Easy" => "1", "Medium" => "3", "Hard" => "5"}, {:selected=>get_difficulty(@tour).to_s}, :class=>"input_text" %> Now class is applied!!! Any other better way pls tel me,,.</span>

Generate random texts

Hi, I have used this method to generate random texts .... def rendom_password chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a newpass = "" 1.upto(7) { |i| newpass end Thanks, sri

Getting Next Existing Record in a DB

Wondered how to do it,, & did it Using offset: sql: SELECT * FROM foo WHERE id = 4 OFFSET 1

MySQL Change root Password

Image
Source : http://www.cyberciti.biz/faq/mysql-change-root-password/ mysqladmin command to change root password If you have never set a root password for MySQL, the server does not require a password at all for connecting as root. To setup root password for first time, use mysqladmin command at shell prompt as follows: $ mysqladmin -u root password NEWPASSWORD However, if you want to change (or update) a root password, then you need to use following command $ mysqladmin -u root -p'oldpassword' password newpass For example, If old password is abc, and set new password to 123456, enter: $ mysqladmin -u root -p'abc' password '123456' Change MySQL password for other user To change a normal user password you need to type (let us assume you would like to change password for vivek): $ mysqladmin -u vivek -p oldpassword password newpass Changing MySQL root user password using MySQL sql command This is another method. MySQL stores username and passwords in user t...

Polymorphic Association

Reference : http://charlesmaxwood.com/ruby-on-rails-restful-links-when-you-dont-know-the-class/ Example : restful routes: map.resources :users map.resources :groups Model Relation: class Post < ActiveRecord::Base belongs_to :owner , :polymorphic => true end class User < ActiveRecord::Base has_many :posts , :as => :owner end class Group has_many :posts , :as => :owner end Now, let’s say that when you show a post, you want to provide a link to the owner of the post when you display it on its show page. You know that because you’ve provided the restful routes in your config/routes.rb file as show above, you get the nice functionality of the user_path and the group_path methods. The problem is that because you don’t know if @post.owner is a user or a group. Documentation : http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M000261&name=polymorphic_path Thanks, Srikanth

base 64 enocde/decode

require 'base64' puts Base64.decode64("aG1hYw==").chomp => hmac puts Base64.encode64("hmac").chomp => aG1hYw==

Check Whether a test Credit Card is working.,

require 'rubygems' require 'active_merchant' # Use the TrustCommerce test servers ActiveMerchant::Billing::Base.mode = :test # ActiveMerchant accepts all amounts as Integer values in cents # $10.00 amount = 1000 # The card verification value is also known as CVV2, CVC2, or CID credit_card = ActiveMerchant::Billing::CreditCard.new( :first_name => 'Bob', :last_name => 'Bobsen', :number => '349298720353895', :month => '8', :year => '2012', :verification_value => '1234' ) puts credit_card.valid?

Getting Local ip Address

require 'socket' def local_ip orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily UDPSocket.open do |s| s.connect '64.233.187.99', 1 s.addr.last end ensure Socket.do_not_reverse_lookup = orig end p= local_ip puts p

Adding data in add column

Adding data in add column class AddPeopleSalary def self.up add_column :people, :salary, :integer Person.reset_column_information Person.find(:all).each do |p| p.update_attribute :salary, SalaryCalculator.compute(p) end end end

Amazon simple Payment in ruby on rails

hi, Amazon is Doing a great job in rails Payments. here is the ruby code for generation of button. require 'base64' require 'openssl' module PayNowWidgetUtils def generate_signed_form(access_key, aws_secret_key, form_params) form_params['accessKey'] = access_key str_to_sign = "" form_params.keys.sort.each { |k| str_to_sign += "#{k}#{form_params[k]}" } digest = OpenSSL::Digest::Digest.new('sha1') hmac = OpenSSL::HMAC.digest(digest, aws_secret_key, str_to_sign) form_params['signature'] = Base64.encode64(hmac).chomp signed_form = STARTFORM form_params.each do |key, value| next unless key and value signed_form += FORMELEM end signed_form += ENDFORM...

Get the list of sundays in a month

require 'date' class Date def self.last_day_of_the_month yyyy, mm d = new yyyy, mm d += 42 # warp into the next month new(d.year, d.month) - 1 # back off one day from first of that month end def self.print_sundays(d1, d2) d1 +=1 while (d1.wday != 0) d1.step(d2, 7) do |date| puts "#{Date::MONTHNAMES[date.mon]} #{date.day}" end end end date = Date.today month = date.strftime("%m").to_i year = date.strftime("%Y").to_i last_date = Date.last_day_of_the_month(year, month) l_date = last_date.strftime("%d").to_i Date.print_sundays(Date::civil(year, month, 1), Date::civil(year, month, l_date))

Connecting Db in ruby

MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB_NAME = 'localhost', 'root', '', 'artiklz_development' @@dbh = Mysql.real_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB_NAME)

calling JS from form in rails

"report"},{:method => :post, :onSubmit => "return isNumeric(document.getElementById('premium_user'))"})%>

CSV

hi, any work under csv?? here s something interesting which makes ur csv work easier. http://fastercsv.rubyforge.org/ Thanks.

concat multiple arrays

hi, joining multiple arrays is easy using "+" we can do that using a single method as follows, class Array def concat_multi *lists lists.each {|list| self.concat(list) } end end a = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9] d = [10, 11] a.concat_multi(b, c, d) p a

Convert a String into Time

require'rubygems' => true require'time' => true a="Thu Feb 19 18:16:33 +0530 2009" => "Thu Feb 19 18:16:33 +0530 2009" a=Time.parse(a) => Thu Feb 19 18:16:33 +0530 2009 a.day => 19 a.year => 2009 a.month => 2 a.hour => 18 a.min => 16 a.sec => 33 thanks..