I canβt think of a purely server-side method, but you can use some hybrid javascript / rails.
assuming you have a dedicated iframe layout template e.g. 'Layouts / iframe.erb
you could put some javascript in your head to check if it loads as an iframe, and if not, redirect to action and possibly display flash msg "can load this page only inside the application"
Javascript / head rails
<script type="text/javascript"> function parentExists() { return (parent.location == window.location)? true : false; }; function check_modal(){ if (parentExists()) { window.location = '<%= url_for( :controller => "home", :action => 'iframe_action', :iframe_fail => 'true')%>'} } check_modal() </script>
pay attention to the parameter: iframe_fail, which you can check in the controller, and do anything if this parameter is present, for example. display flash message or redirect
sample controller
def iframe_action if params[:iframe_fail] flash[:notice] = 'can only load inside app' else #do something else end end
Not entirely beautiful, but it can help you get the job done.
source share