balanv
balanv

Reputation: 10898

Issue with using CDN and https: (Rails)

In my Rails application, i have used CDN. I have configured the cdn by adding cdn url to

config.action_controller.asset_host = "http://cdn.mydomain.com"

in production.rb file.

Now i am trying to have https:// for certain pages like Sign In and Sign Up

But as the assets are served from CDN, the https conflicts with the cdn path.

My solution to this to make the sign in and sign up pages not to use the cdn assets and should point as local assets.

is my solution correct? if so how do i restrict certain layout files from using CDN asset path?

Upvotes: 0

Views: 542

Answers (2)

balanv
balanv

Reputation: 10898

NOTE : The code request.try(:ssl?) always returns false even when i run the https version. I am working on finding the solution, will post it once i find it.

Found the solution

config.action_controller.asset_host = Proc.new do |*args|
  source, request = args
  if request.try(:ssl?)
    'https://mydomain.com'
  else
    'http://cdn.mydomain.com'
  end
end

Upvotes: 0

Jeff Steil
Jeff Steil

Reputation: 1770

I would look at this response: Configure dynamic assets_host in Rails 3

What I think you would want to do is change asset_host to be dynamic based on whether your page is served over https or not. Something like:

config.action_controller.asset_host = Proc.new { |source, request|
"#{request.ssl? ? '/assets' : 'http://cdn.mydomain.com'}"

}

My syntax may be a little off as I'm typing it up on the fly but it should be close to what you need.

Upvotes: 2

Related Questions