render in rails

June 13, 2008

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

Entry Filed under: Rails. Tags: , , , .

1 Comment Add your own

Leave a Comment

hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories

posts[:recent]

episodes[:recycled]

@@name = PRAYAS

Step down at my blog with your ideas,comments,suggestions on Ruby,RoR,Ajax or Web2.0.You may reach me at
infostall@gmail.com

find_by_tags

Links

visitors[:since_Mar'08]

free web counter

Spam Blocked

Feeds

Meta

RSS Prayas here