I’m trying to change the layout of my application according to a GET parameter passed in the URL of each request : &layout=name_of_the_layout.
And in my 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
It works fine, but to “persist” the layout when the the user navigates in the application, I need to add this parameter on each rails route helper in my views (even for POST requests in forms…):
ressource_path(@ressource, :layout => get_layout_from_url())
where get_layout_from_url() is a helper that checks if the params['layout'] is set in the URL, validates then returns it.
This is definitely not DRY… How can i override every route helper to include this behavior without writing any additional code in my views? I would like to call the standard rails methods in my views : ressource_path(@ressource), ...
Or is there a smarter way to achieve this?
PS : Im using rails 3.2.3
Thanks !
There was the old and deprecated
default_url_optionsnow replaced byurl_options:If it doesn’t fit, it is close.