Skip to main content

Generate Random string in Ruby On Rails

##method 1
def random_password(size = 6)
chars = (('a'..'z').to_a + ('0'..'9').to_a) - %w(i o 0 1 l 0)
(1..size).collect{|a| chars[rand(chars.size)] }.join
end
 puts random_password.inspect

##method 2
 def newpass(len=6)
  chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  newpass = ""
  upto(len) { |i| newpass << chars[rand(chars.size-1)] }
 return newpass
end
p newpass.inspect

##method 3
def generate_string(a)
new_code = random_string(6)
end
p  new_code.inspect

###correct_method
##method 4
def random_string(length = 6)
chars = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
(1..length).map {chars[rand(chars.length)]}.join
end
p random_string.inspect

##method 5
def random_string(length = 6)
rand(36**length).to_s(36)
end
 p random_string.inspect

##method 6
def random_string
 o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
 string = (0..50).map{ o[rand(o.length)] }.join
end

##method 7
def generate_activation_code(size = 6)
charset = %w{ 2 3 4 6 7 9 A C D E F G H J K L M N P Q R T V W X Y Z}
(0...size).map{ charset.to_a[rand(charset.size)] }.join
end
p generate_activation_code.inspect

##method 8
def generate_random_string(length=6)
 string = ""
 chars = ("A".."Z").to_a
 length.times do
 string << chars[rand(chars.length-1)]
end
string
end
p generate_random_string.inspect

##method 9
def random_alphanumeric(size=16)
      chars = ('a'..'z').to_a + ('A'..'Z').to_a
    (0...size).collect { chars[Kernel.rand(chars.length)] }.join
end

##method 10
def random_string
    chars = ("a".."z").to_a + ("1".."15").to_a
    newpass = Array.new(8, '').collect{chars[rand(chars.size)]}.join
end
 p random_string.inspect

Comments

Popular posts from this blog

Get the Current url in ruby on rails

request.refererrequest.fullpathrequest.fullpath.split("?")[0]request.referer or request.env['HTTP_REFERER'] your controller to get the referer url. 

--------------------
request.original_url
request.url
request.host_with_port
<link rel="canonical" href="<%= request.url %>" />
<%=request.path%>
How can I get the current absolute URL in my Ruby on Rails view?
The request.request_uri only returns the relative URL.
request.env['REQUEST_URI']

 For Rails 3.2 or Rails 4 you should use request.original_url to get the current URL. More detail.

For Rails 3: You want "#{request.protocol}#{request.host_with_port}#{request.fullpath}", since request.url is now deprecated.

For Rails 2: You want request.url instead of request.request_uri. This combines the protocol (usually http://) with the host, and request_uri to give you the full address

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).?

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).) In rails?


1)change the gemfile in mysql version
 gem 'mysql2'

2)Change to mysql2 version
 gem 'mysql2','0.3.20'

Active Scaffold : Add radio buttons to your form ruby on rails ?

Active Scaffold : Add radio buttons to your form ruby on rails ?

The way to add radio buttons to your active scaffold create/update from is as shown below

#Controller

class UsersController < ApplicationController
active_scaffold :user do |config|
config.label = “Users”
config.columns = [:name, :gender]
config.columns[:gender].form_ui = :radio
config.columns[:gender].options[:options] = [['Male', '1'], ['Female','2']]
end
end