是否可以設置從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中指定的模板。 –