是否可以设置从before_filter渲染的视图?Rails设置视图在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其设置模板运行( '产品/ view1.html.erb'); 2.执行一些逻辑并设置@variables的动作运行(方法'action1'); 3.它呈现before_filter中指定的模板。 –