Posts Tagged RoR
Generate Unique ID
Here is a very simple way to generate a unique id.
[Background support : Abhishek S.]
def generate_unique_id( len )
chars_pattern = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
unique_id = ""
1.upto(len) {
|i| unique_id << chars_pattern[rand(chars_pattern.size-1)] }
return unique_id
end
#generates a key of length 10
mykey = generate_unique_id(10)
print mykey # gives a result like "qeKX0myIQh"
Add comment June 23, 2008
render in rails
What do you see in a browser is nothing but a response; which is a combination of header and some document data.
From where does it come? -> Through the action of some controller.
Each action results in a response,
response = headers + document content
This resonse object is generated using various types of renders/redirects. Action Controller sends content to the user by using these rendering methods.
By default, actions are rendered within the current layout (if one exists)
- AUTOMATIC RENDER
def myname
@name = “rajesh”
end
myname.rhtml ->My name is “#{@name}”
Yes this is also a type of render; an automatic rendering using instance var.
- SIMPLE RENDER
def method1
@val = 30
render :action => “method2″, :layout => “mylayout1″
return
end
> This will execute the view of method2,
> Nothing will appear as a view for method1
> there is no relation what all defined in action def method2
> Any instance var defined before this statment can be executed in the view for method2 (not in action)
means in readered view of method2.rhtml
puts @val # returns you 30
where as
def method2
puts @val # dont expecct 30 in direct way
end
> TWO render is not possible in same action, else gives error, coz control exececutes code after the render statement.
- RENDER PARTIAL
def method1
@val = 30
render :partial => “my_first_partial”
return
end
> render partial can be used in both controller as well as view, while plain render can be used in controller only
> In controller if you are using -> render :partial => “my_first_partial”
Layout will be lost of the current method i.e method1; But if you want to preserve it use layout as true
render :partial => “my_first_partial”, :layout => ‘true’
> If you are using it in view then layout of method1 will be preserved.
> instance var is well accessible in partial file, but if you want to pass the local var, then must use locals
render :partial => “my_first_partial” , :locals => {:q => 90}
- RENDERING TEXT
render :text => “hello”, :layout => true
render :inline => “<%= ‘hello, ‘ * 3 + ‘again’ %>”, :layout => true
- REDIRECTS_TO
redirect_to refers the method while render seeks you to the corresponding view.
You cant have these two together without any conditional statment.
RENDER -> populates a VIEW
REDIRECT -> hammers an ACTION
[Background support : Himanshu P.]
1 comment June 13, 2008
Sorting an Array of hashes
We get a situation where we fetch some set of records from db as an array of hashes, and later we need to sort this array based on some field of DB.
Here is a very hand code to do so.
array_objs =[ {:title => "row1_title" ,:name => "Mumbai"},
{:title => "row2_title" ,:name => "Delhi"},
{:title => "row3_title" ,:name => "Bangalore"} ]
array_objs .sort_by {|array_objs| array_objs [:name] }.each do |res|
puts "#{res[:name] }"
end
O/P:
Bangalore
Delhi
Mumbai
Add comment May 13, 2008
Heroku & Morph AppSpaces : Rails Hosting
Heroku and Morph Labs are Ruby on Rails hosting providers, offering a complete environment for running Ruby on Rails applications. Compared to traditional hosters, they don’t just give you a server but offer some interesting tools and interfaces to make your live easier and relieve you from all the hassle of installing, configuring, managing and securing a server. Both are using the Amazon EC2 grid computing technology to run the
applications, so you don’t have to worry about scaling and performance issues either.

James Lindenbaum explains the unique features of Heroku:
“Heroku is hands down the easiest deployment platform for Rails apps. No humans in the loop, just drop in your code and you’re up and running. Heroku handles everything, from version control and collaboration to auto-scaling (built on top of Amazon’s EC2). We offer a full suite of tools for developing and managing your app, through either the web
interface or our new external API.“
Asked about their target audience, James replied:
“Honestly, almost everyone who wants to develop or deploy Rails apps should use Heroku. About a third of our users are beginners (many of whom haven’t written a web app before at all), and they love it because they can get up and running instantly. Another third of our users are more serious Rails developers, who just don’t want to deal with the hassles of setup, configuration, and deployment. We have started to offer features for this group over the last couple of months, and thousands of these users have been happily banging away on our private beta. The last third of our users are really hardcore Rails developers.
We’ve just begun to offer features for this group (the API, external access to Git code repositories), and have many more to be released in the coming months. It’s this hardcore group that has accounted for the largest part of the load on our platform over the last 6 weeks. “
Morph eXchange is a portal into Morph Labs’ Software as a Service offerings. The Morph AppSpaces can be found in their DevCenter. We asked Macel Legaspi from Morph Labs about the characteristics of their offering:
Morph AppSpaces are a Platform as a Service for Ruby on Rails applications. The Morph AppSpace provides all of the technology and infrastructure needed to deploy, deliver and manage a web application written in Ruby on Rails. Each Morph AppSpace provides a highly available, fault tolerant environment for a web application. Load balancers, distributed application servers, integrated web delivery stacks, security and managed backups are all part of every Morph AppSpace. A Morph AppSpace subscription is near nirvana for a Ruby on Rails web application developer. The Morph AppSpaces run on top of the Morph Application Platform. The Morph Application Platform brings together a collection of open source technologies and Morph intellectual property to provide the Morph AppSpace environment. The Morph Application Platform uses cloud computing including Amazon EC2 and Amazon S3 to provide computer power and online storage as needed by the Morph AppSpaces.
Add comment April 16, 2008
do while in ruby
How to achieve the do ..while loop in Ruby?
Yes this can be, just bit tricky.
Just consider one of the syntax of while loop in ruby
<expression> while <expression>
This repeats evaluation of left hand side expression, while right hand side is true.
x = 5 begin print x print " " x += 1 end while(x<10) o/p 5 6 7 8 9
Add comment April 7, 2008
blank,empty and nil in Ruby
Object.blank?
Returns true if:
> it’s an empty array
> it’s an empty string
> !self evaluates to true
[].blank? #=> true
[1].blank? #=> false
[nil].blank? #=> false
nil.blank? #=> true
true.blank? #=> false
false.blank? #=> true
"".blank? #=> true
" ".blank? #=> true
" i ".blank? #=> false
String.empty?
"hello".empty? #=> false
"".empty? #=> true
Array.empty?
[].empty? #=> true
Hash.empty?
{}.empty? #=> true
NilClass.nil?
nil.nil? => true
Object.nil?
nil.nil? => true
.nil? => false
nil.inspect ? "nil"
Add comment April 5, 2008