我試圖根據每個請求的URL中傳遞的GET參數更改我的應用程序的佈局:&layout=name_of_the_layout
。覆蓋rails輔助路由方法來設置GET參數的佈局
在我application_controller:
class ApplicationController < ActionController::Base
layout :get_layout_from_params
private
def get_layout_from_params
if params['layout']
params['layout']
else
'application'
end
end
end
它工作正常,但是當用戶在應用程序導航「堅持」的佈局,我需要在我的意見加上每個軌道路線助手此參數(即使在表單POST請求...):
ressource_path(@ressource, :layout => get_layout_from_url())
其中get_layout_from_url()
是,如果params['layout']
在URL設置,檢查一個幫手,驗證然後返回。
這絕對不是DRY ...我怎麼能重寫每個路由幫助包括這種行爲,而不寫我的意見中的任何額外的代碼?我想在我的意見中稱爲標準導軌方法:ressource_path(@ressource), ...
或者是否有更聰明的方法來實現?
PS:使用導軌的Im 3.2.3
謝謝!
正是我在尋找的感謝! – Tricote