class HomeController < ApplicationController
layout :set_layout
def index
@newalert = Newalert.find(:all)
render :partial=>"newalert/new_alert",:layout=> :set_layout
end
protected
def set_layout
logged_in? ? "application" : "home"
end
end
Refactorings
No refactoring yet !
Jeremy Weiskotten
July 31, 2008, July 31, 2008 14:08, permalink
Partials don't use the layout by default. It seems strange that you'd want to render a partial from your index action... I'm guessing that's not ultimately what you want to do.
DG
August 1, 2008, August 01, 2008 04:57, permalink
Hi Jeremy
Thanks for your explanation. Yes you are right I am not rendering partial from my action.
But is it wrong to do so? because in other many application code I seen the same thing.
Thanks
DG
cpjolicoeur
August 3, 2008, August 03, 2008 22:55, permalink
If you see partials being rendered directly in controller actions in other code, well, that other code is bad as well.
You really generally only render partials from view files, not directly from controller action methods.
chovy.myopenid.com
October 5, 2008, October 05, 2008 02:18, permalink
I think you just need to call it as a function call, instead of a symbol.
Try this...
layout :set_layout # might be just set_layout
render partial 'new_alert', :layout => set_layout
def set_layout
logged_in? ? "application" : "home"
end
In below code snippet.
- I need to compulsory pass an layout option to render partial line. As you can see I am all ready setting the layout in set_layout method.
- If I remove the (:layout=> :set_layout) from render line then it doesn't render the proper layout.
If anyone explain me why it's happening like that or am I doing something wrong?
Thanks
DG