Archive for June, 2008

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"

June 23, 2008 at 11:47 am Leave a comment

code optimization

if params[:a].nil?
  if params[:b].nil?
       "999"
  else
     params[:b]
  end
else
  params[:a]
end

the same can be written using ternary operator

@a1 = params[:a].nil? ? params[:b].nil? ? ’999′ : params[:b] :params[:a]

But you know RoR is so powerful…..
@a1 = params[:a] ||= params[:b] ||= ’999′
[Background support : Ashish S.]

June 18, 2008 at 11:09 am Leave a comment

Agile web development 3rd Edition

DHH’s Agile web development – 3rd Edition is now in the market covering RAILS 2. Hope again this will rock.

DHH\'s Rails BOOK

June 18, 2008 at 10:58 am Leave a comment

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.]

June 13, 2008 at 8:40 pm 1 comment


Categories

posts[:recent]

@@name = PRAYAS

This blog has more intention for my self reference@24×7. But if some search engine crawler directs you to my page, believe I may learn form your comments or feedback. Hence feel free to comment good or bad!
prayas2010@gmail.com

visitors[:since_Mar'08]

free web counter

Prayas@Nikon

A colored life

Chhweet Kidss

An independent world

More Photos

Feeds


Follow

Get every new post delivered to your Inbox.