Arwed
Arwed

Reputation: 1755

Different layout for iframe

I have a rails app with content other websites need to access via iframe. The content should have a different layout when shown on the websites (no menu bar etc.) I made a new layout file called iframe.html.erb How can I check, whether the page is called form an external iframe so the right layout file is used?

Upvotes: 4

Views: 3354

Answers (3)

Chris C.
Chris C.

Reputation: 311

A good way to control the specific layout and content when serving an iframe is to register an "iframe" mimetype.

## config/initializers/mime_types.rb

Mime::Type.register 'text/html', "iframe"

Create a view that matches the controller action being served ie: show.iframe.haml. Then, when a request comes in with format: iframe it'll render the iframe version.

That way you can control exactly what's in the iframe on other sites. No need to get crazy in the controller.

Upvotes: 5

Adrian Serafin
Adrian Serafin

Reputation: 7715

As far as I know when you doing

<iframe src="www.google.pl"></iframe>

you have no control over layout or styles of page display in iframe unless you own the page and can make it look whatever you like.

EDITED

If you displaying your own site go like this:

<iframe src="/some_site_that_i_can_change_code_in?from=iframe"></iframe>

and then in controller of some_site_that_i_can_change_code_in:

if params[:from] == "iframe"
  render :layout => "for_iframe"
else
  render :layout => "normal"
end

Upvotes: 8

ice cream
ice cream

Reputation: 2470

I think the only way to do this is with Javascript and then redirect, but it's kind of messy and not really a good idea. See this thread for more info: Detecting if this is an iframe load or direct

Upvotes: 0

Related Questions