【发布时间】:2013-12-04 10:49:09
【问题描述】:
要在 rails4 中使用 iframe,是否需要进行任何额外的配置?
升级到 rails 4 iframe 后,我的应用程序无法在很多地方使用。 对此有任何想法吗? 我在 rails4 中使用 iframe 和表单。也在 jquery 中提交。
【问题讨论】:
-
为什么不工作?更多信息
要在 rails4 中使用 iframe,是否需要进行任何额外的配置?
升级到 rails 4 iframe 后,我的应用程序无法在很多地方使用。 对此有任何想法吗? 我在 rails4 中使用 iframe 和表单。也在 jquery 中提交。
【问题讨论】:
Rails 4 added 默认X-Frame-Options HTTP 标头值为SAMEORIGIN。这有利于安全性,但是当您确实希望在 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
当您需要在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
【讨论】:
这里已经回答了:Ruby on rails 4 app does not work in iframe
Rails 4 中有一个新的默认设置,默认添加一个标题:
config.action_dispatch.default_headers = {
'X-Frame-Options' => 'SAMEORIGIN'
}
如果你想恢复到以前的行为,只需在 config/application.rb 中添加以下内容
config.action_dispatch.default_headers = {
'X-Frame-Options' => ''
}
【讨论】: