# Checking for SSL in a view

**URL:** <https://www.ruby-forum.com/t/checking-for-ssl-in-a-view/185935>\
**Category:** Rails\
**Created:** [March 29, 2010, 2:03pm UTC](https://www.ruby-forum.com/t/checking-for-ssl-in-a-view/185935 "2010-03-29T14:03:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjmorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@pjmorse](https://www.ruby-forum.com/u/pjmorse)\
**Post date:** [March 29, 2010, 2:03pm UTC](https://www.ruby-forum.com/t/checking-for-ssl-in-a-view/185935/1 "2010-03-29T14:03:26Z")

</div>

Here’s the situation:

- I have a site where a few pages are ssl-required but the whole site  
is ssl-allowed. (In other words, most of the site can be reached by  
either https or http.)

- In order to minimize user’s browser warnings, I want to use https  
for all assets on pages when the request is https. This is easy for  
requests on the same host (e.g. css, javascript since we are not using  
a separate asset host yet) but some images are hosted by an off-site  
server.

- These images have https versions available, but on a different host,  
e.g. regular images are at [http://images.example.com/images/filename.jpg](http://images.example.com/images/filename.jpg)  
but secure at [https://ssl-images.example.com/images/filename.jpg](https://ssl-images.example.com/images/filename.jpg). I  
have tweaked my image-URL-producing functions to produce the right  
image URL when they are asked, e.g. def get\_image\_url(ssl=false), so  
get\_image\_url returns [http://images.example.com](http://images.example.com) and  
get\_image\_url(true) returns [https://ssl-images.example.com](https://ssl-images.example.com).

How do I know, inside a view, when to ask that function for SSL and  
when not?

- request.ssl? doesn’t appear to be available inside views. (Not  
surprising given that it’s part of ActionController.)

- I tried setting a global variable inside the controller (e.g.  
@ssl\_request = request.ssl?) but for some reason that’s not working,  
either.

It seems as though Rails is not getting the message that the request  
is SSL. I’m using Nginx + Passenger for hosting; is there something  
similar to the “proxy\_set\_header X-FORWARDED\_PROTO https;” which  
should be used when the request isn’t being proxied?

Thanks,

pjm
