Rails 3干燥iframe的条件布局 [英] Rails 3 DRY Conditional Layout for iframes

查看:117
本文介绍了Rails 3干燥iframe的条件布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近的任务是在另一个网站上的iframe中加载我的部分Rails应用程序。相关页面应该使用不同的布局文件,但前提是它们是在iframe内部呈现的。这里提出了一个解决方案在rails应用中检测iframe请求涉及传递查询字符串参数。

I was recently tasked with loading a portion of my Rails application within an iframe on another website. The relevant pages should be using a different layout file, but only if they're being rendered inside of the iframe. There was a solution proposed here Detect iframe request in a rails app that involved passing a query string parameter.

例如,请求网站可以通过iframe调用我的应用程序,其中src为 http://foo.com/bar?iframe=true 。然后在我们的控制器中我们可以简单地检查:

For example the requesting website could call my application through an iframe with the src of http://foo.com/bar?iframe=true. Then in our controller we could simply check:

def bar
  render :template => "iframe" if params[:iframe]
end

这似乎是一个很好的解决方案,但遗憾的是,这只适用于初始请求,因为原始查询字符串是完全静态的。假设我们有iframe中其他路由的可访问链接,是否有任何方法可以轻松转发 iframe = true 请求参数以维护正确的iframe布局而无需重复代码?基本上我想在不破坏任何现有功能的情况下采用DRYest方法。我考虑创建另一个link_to帮助器,其中包含用于中继此参数的逻辑(如果存在)并替换我的整个应用程序中的所有link_to调用;我想知道是否有人有更好的方法。

This seems like a good solution, but sadly that only works for the initial request as the original query string is completely static. Assuming we have accessible links to other routes within the iframe is there any way of easily relaying the iframe=true request parameter to maintain the correct iframe layout without having to repeat code? Basically I would like to take the DRYest approach possible without breaking any existing functionality. I considered creating another link_to helper which included the logic to relay this parameter if it exists and replacing all of my link_to calls throughout my application; I was wondering if anybody had a better approach though.

推荐答案

我决定使用JavaScript解决这个问题,并将以下内容添加到我的haml布局文件:

I decided to tackle this problem using JavaScript and added the following to my haml layout file:

:javascript
    for(i = 0; i< $('a').length; i++)
    {
      if($('a')[i].href.match(document.domain))
      {
        $('a')[i].href = $('a')[i].href + "?iframe=true";
      }
    }

这与我对iframe的服务器端检查相结合param将确保加载适当的布局。我决定只为启用JavaScript的用户提供此功能,因此它可能不是最佳解决方案。这种方法唯一的另一个问题在于控制器重定向和表单,我必须手动检查iframe参数,然后相应地转发它 - 根本不是DRY,但我至少能够将逻辑放入控制器方法中。如果有人知道更好的解决方案,请随时留下答案。

This coupled with my server-side checks for the iframe param will ensure that the appropriate layout is loaded. I decided to only cater this functionality to users who enable JavaScript so it might not be the best solution. The only other problem with this approach lies in controller redirects and forms where I have to manually check for the iframe param and then forward it accordingly - not DRY at all, but I was at least able to put the logic into a controller method. If somebody knowns of a better solution please feel free to leave an answer.

这篇关于Rails 3干燥iframe的条件布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆