在rails里动态调用layout

rails里有时候我们有需求:在不同条件下用不同的layout,而不仅仅是在不同的方法用不同的layout

简单的针对方法调用不同的layout的办法如下
[ccn lang="ruby" tab_size="4" theme="blackboard" width="800" ]
class HomeController < ApplicationController layout "empty", :except => [:i_want_credits, :translation_match, :notfound, :errorpage]
[/ccn]
如果在方法里用条件判断的时候怎么办呢
http://guides.rubyonrails.org/layouts_and_rendering.html里有这么一段
2.2.11 Options for render

Calls to the render method generally accept four options:

:content_type
:layout
:status
:location

于是可以这样
[ccn lang="ruby" tab_size="4" theme="blackboard" width="800" ]
@dl_threads = DlThread.where(" ispass = 1 and ( name like :search_string or ispass = 1 and content_desc like :search_string )",
{:search_string => @search_string}).order("created_at DESC").paginate(:page=>params[:page]||1,:per_page=>10)
if @dl_threads.empty?
flash.now[:message] = "对不起,没有搜索到相关内容,您可以在右侧点击我要上传来添加这个资源,可以获取大量积分和金币"
render 'dl_search.html', :layout => 'application'
return
end
# 拿出最多上传的用户
#@rank_users = User.find_by_sql('select * from users LEFT OUTER JOIN user_counts ON user_counts.user_id = users.id where user_counts.uploads != 0 order by user_counts.uploads desc limit 0, 12')
# 拿出最热下载
#@hot_dl_threads = DlThread.where("ispass = ?", 1).order("views DESC").limit(10).offset(0)
# 记录用户搜索的记录
save_user_keyword 'dl'
render 'dl_search.html', :layout => 'application'
return
[/ccn]