简体   繁体   English

在Rails中将参数传递给iframe不起作用

[英]Passing params to iframe in rails not working

I'm a rails noob so I know I'm probably totally missing something here. 我是Rails新手,所以我知道我可能在这里完全错过了一些东西。 I'm trying to pass a url to an iframe through my products controller. 我正在尝试通过产品控制器将网址传递给iframe。

This is my setup. 这是我的设置。

Products Controller 产品总监

    def open_url
        @url = params[:url]
    end

index.html.erb index.html.erb

<%= link_to "More Info", open_path(url: "http://www.ceratoboutique.com" + product.destination_url) %>

open_url.html.erb open_url.html.erb

<iframe src= "<%= @url %>" style="border: 0; position:fixed; top:0; left:0; right:0; bottom:0; width:100%; height:100%" /> 

routes.rb routes.rb

get '/open' => 'products#open_url', via: 'get'

I,ve checked out these two questions 我已经检查了这两个问题

Rails 4 - Passing Params via link_to? Rails 4-通过link_to传递参数?

Opening a Link in a New Window within an iFrame 在iFrame中的新窗口中打开链接

but i'm still lost, the url is passed to the browser but it does not seem to pass to the @url variable in my controller. 但我仍然迷路,该url被传递到浏览器,但它似乎没有传递给控制器​​中的@url变量。

Debug Dump 调试转储

!ruby/hash:ActionController::Parameters !ruby / hash:ActionController :: Parameters

url: http://www.ceratoboutique.com/collections/tops/products/combo-blouse 网址: http : //www.ceratoboutique.com/collections/tops/products/combo-blouse

controller: products 控制器:产品

action: open_url 动作:open_url

I decided to stick to rails conventions and make it a restful link. 我决定坚持使用Rails约定,并使其成为一个宁静的链接。 I still do not know why the original implementations did not work, but it worked using the show method in the controller. 我仍然不知道为什么原始的实现不起作用,但是使用控制器中的show方法可以起作用。

Controller 控制者

def show
    @url = Product.find(params[:id])
end

index.html.erb index.html.erb

<%= link_to "More Info", product_path(product) %> 

show.html.erb show.html.erb

<iframe src= "<%= "http://www.ceratoboutique.com" + @product.destination_url %>" style="border: 0; position:fixed; top:0; left:0; right:0; bottom:0; width:100%; height:100%" /> 

****Edit Added More Info for including I-Frame **** ****编辑添加了更多信息,包括I框架****

I ran into a lot of problems trying to get my iframe to work in chrome and on Heroku so I combined the process if anyone needs it ever. 我遇到很多问题,试图让我的iframe在chrome和Heroku上运行,因此如果有人需要,我会合并该过程。 I first deployed to Heroku with full SSL running on my site, then realized that iframe did not work in chrome for sites that were not running SSL. 我首先在网站上运行完整SSL的情况下部署到Heroku,然后意识到对于未运行SSL的网站,iframe在chrome中不起作用。 I redeployed configuring force SSL to false, but heroku still forced my app to SSL. 我重新部署了将Force SSL设置为false的配置,但是heroku仍然将我的应用程序设置为SSL。 I realized that config.force_ssl = true enables Strict Transport Security header(HSTS) with max-age of one year, so I had to expire HSTS using the following. 我意识到config.force_ssl = true启用严格传输安全标头(HSTS),最大使用期限为一年,因此我必须使用以下方法使HSTS过期。

Expire SSL in application controller 在应用程序控制器中使SSL过期

class ApplicationController < ActionController::Base
  before_filter :expire_hsts

  def expire_hsts
    response.headers["Strict-Transport-Security"] = 'max-age=0'
  end 

In Production.rb 在Production.rb中

config.force_ssl = false 

Then to make sure the x-frame showed in chrome browsers I added the following. 然后,为了确保在chrome浏览器中显示x框架,我添加了以下内容。

enable x-frame in chrome 在Chrome中启用X框架

config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOWALL' }

You may want to run SSL on some of your pages, which can be done rather easily via the SSL enforcer gem linked below. 您可能希望在某些页面上运行SSL,这可以通过下面链接的SSL强制程序gem轻松完成。

ssl-enforcer gem ssl-enforcer宝石

https://github.com/tobmatth/rack-ssl-enforcer https://github.com/tobmatth/rack-ssl-enforcer

Best of luck on navigating the ugliness that is the iFrame! 祝您好运,导航到iFrame的丑陋之处!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM