Thursday, June 14, 2007

Rails - render :action vs render :template

render :action vs render :template

One can use render :action to render the template associated with another action.

However, when the user does

def action1
@foo = "action1"
render :action => "action2"
end

def action2
@foo = "action2"
render
end

the user should realize the method associated with action2 is not exectued when action1 calls render :action => "action2". This is simply the equivalent of render :template => "my_controller/action2".

This can get confusing when action2 in turn uses a non-default template to render itself.

def action1
@foo = "action1"
render :action => "action2"
end

def action2
@foo = "action2"
render :template => "some/other"
end



In this case, when action1 is executed, rails will return an error complaining "views/my_controller/action2.rhtml" was not found.

4 comments:

Anonymous said...

Good to know... But the title is misleading

What is the difference between
render :template => "my_controller/fooaction"

and

render :action => 'fooaction'

?

lobo_tuerto said...

Yeah, explain this better please.

Caio Tiago said...

There is no difference, anonymous.

The post says this.

That's syntactic sugar.

caike said...

use render :action => 'name' when the action lies within the same controller and the template within the controller's folder in views.

use render: template => 'other/action' when you're rendering a template that lies within another controller's folder.