Ruby on rails 4应用程序在iframe中不起作用
我如何通过iframe将我的rails应用程序embedded到另一个网站?
它与RoR 3很好地配合,但与RoR 4无关:
<iframe src="http://myrailsapp.com/" width="100%" height="50" id="rails_iframe">error!</iframe>  我试图在我的控制器中使用verify_authenticity_token和protect_from_forgery选项…似乎是别的东西(但我不确定)。 
UPD。 例如: http : //jsfiddle.net/zP329/
这与Rails 4默认启用额外的安全协议有关: http : //weblog.rubyonrails.org/2013/2/25/Rails-4-0-beta1/
断开远程站点上的iFrame的设置是X-Frame-Options。 默认情况下,它被设置为SAMEORIGIN,这可以防止内容加载跨域:
 config.action_dispatch.default_headers = { 'X-Frame-Options' => 'SAMEORIGIN' } 
你可以在这里阅读新的默认标题: http : //edgeguides.rubyonrails.org/security.html#default-headers
为了允许iFrame跨域工作,您可以更改默认标头以允许跨域的X-Frame。
 config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOWALL' } 
  Rails 4 添加了SAMEORIGIN的默认X-Frame-Options HTTP标头值。 这对安全性很好,但是当你想要在iframe调用你的action ,你可以这样做: 
允许所有的起源:
 class MyController < ApplicationController def iframe_action response.headers.delete "X-Frame-Options" render_something end end 
允许特定来源:
 class MyController < ApplicationController def iframe_action response.headers["X-FRAME-OPTIONS"] = "ALLOW-FROM http://some-origin.com" render_something end end 
使用:after_filter
 当您需要在iframe使用多个action时,最好制作一个方法并使用:after_filter : 
 class ApplicationController < ActionController::Base private def allow_iframe response.headers.delete "X-Frame-Options" end end 
像这样在你的控制器中使用它:
 class MyController < ApplicationController after_filter :allow_iframe, only: [:basic_embed, :awesome_embed] def basic_embed render_something end def awesome_embed render_something end # Other Actions... end 
Via: Rails 4:让特定的动作作为iframeembedded