【问题标题】:rails 4 with iframe not working in rails 4?带有 iframe 的 rails 4 不能在 rails 4 中工作?
【发布时间】:2013-12-04 10:49:09
【问题描述】:

要在 rails4 中使用 iframe,是否需要进行任何额外的配置?

升级到 rails 4 iframe 后,我的应用程序无法在很多地方使用。 对此有任何想法吗? 我在 rails4 中使用 iframe 和表单。也在 jquery 中提交。

【问题讨论】:

  • 为什么不工作?更多信息

标签: iframe ruby-on-rails-4


【解决方案1】:

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

使用 :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

通过:Rails 4: let specific actions be embedded as iframes

【讨论】:

    【解决方案2】:

    这里已经回答了: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' => ''
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 2014-03-11
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 2013-04-19
      • 1970-01-01
      相关资源
      最近更新 更多