【发布时间】:2015-04-07 02:56:03
【问题描述】:
是否可以设置从 before_filter 渲染的视图?
class ProductsController < ApplicationController
before_filter :set_special_view
def action1
# some logic
# i do not want to call render here
end
def action2
# some logic
# i do not want to call render here
end
private
def set_special_view
#self.class.view 'products/v1.html.erb' if some_cond?
#self.class.view 'products/v2.html.erb' if anoter_cond?
#self.class.layout :somelayout if some_cond?
end
end
布局可以做为
self.class.layout :somelayout if some_cond?
有设置视图的方法吗?所以我在 before_filter 中设置了视图,我不触摸动作。
【问题讨论】:
-
流程如下: 1. before_filter 运行哪个设置模板('products/view1.html.erb'); 2.动作运行(方法'action1')执行一些逻辑并设置@variables; 3. 渲染 before_filter 中指定的模板。