内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
我如何通过iframe将我的rails应用程序嵌入到另一个网站?
它适用于RoR 3,但不适用于RoR 4:
<iframe src="http://myrailsapp.com/" width="100%" height="50" id="rails_iframe">error!</iframe>
这与Rails 4默认启用附加安全协议有关:...
在远程站点上中断iFrame的设置是X框架选项。默认情况下,这将设置为SAMEORIGIN,这将防止内容加载跨域:
config.action_dispatch.default_headers = { 'X-Frame-Options' => 'SAMEORIGIN' }
为了允许iframe跨域工作,可以更改默认的标头以允许跨域的X帧。
config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOWALL' }
Rails 4
添加了默认的X-Frame-Options
HTTP头部值SAMEORIGIN
。这对安全性有好处,但是当你确实希望你action
被iframe调用时,可以这样做:
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
当你需要使用一个以上的action
在iframe
,这是一个好主意,使一个方法,并调用它: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