2017-04-05 79 views
1

我想讓用戶將我的應用中的內容嵌入到他們的網站中。爲此,我嘗試獲得以下解決方案:Allow users to embed my content into their sites (like blogs) -- rails 4允許用戶將我的內容嵌入到他們的網站 - rails 4(後續)

不幸的是,我遇到了embed.js中的iframe.src問題,因爲它試圖訪問用戶文件上的「embed」文件夾位置:

GET file:///.../embed/1 net::ERR_FILE_NOT_FOUND

#public/embed.js 
window.onload = function() { 

    //Params 
    var scriptPram = document.getElementById('load_widget'); 
    var id = scriptPram.getAttribute('data-page'); 

    /iFrame 
    var iframe = document.createElement('iframe'); 
    iframe.style.display = "none"; 
    iframe.src = "embed/" + id; 
    document.body.appendChild(iframe); 
}; 

如果我改變iframe.src一個絕對URL指向我的應用程序如「http://localhost:3000/embed」,然後我碰到一個不同的錯誤:

Refused to display 'http://localhost:3000/embed/1' in a frame because it set 
'X-Frame-Options' to 'SAMEORIGIN'. 

很抱歉,如果這個問題似乎是隨機的,我對我的第一個步驟,紅寶石上軌。我答應改善:)

回答

0

您需要修改你的控制器代碼,我希望它會工作 -

class Embed::PagesController < ApplicationController 
layout false 
after_action :allow_iframe, only: :show 

def show 
    @page = Page.find params[:id] 
end 

private 

def allow_iframe 
    response.headers.except! 'X-Frame-Options' 
end 
end 
+1

非常感謝 - 得到它這樣的工作! – bstone

相關問題