Hey, I'm allowing my app to be accessed via https and http, and everything is working fine except for when I use any of the _url
methods. If I access a view that uses such a method via https, it does get served as https, but the generated url uses the http protocol. I'm wondering if this is normal, or if there is a way to make it automatic. I was hoping rails would automatically generate the appropriate url based on how the page was accessed.
If there's no way around this, what would be the best way to create the appropriate url? Would this work?
if request.ssl?
some_url(:protocol => "https")
else
some_url
end
I would prefer it if I could come up with a more automatic approach. Maybe if the rails url methods generated ://somedomain.com/some/path
, that way I imagine it would automatically adapt the correct protocol.
Thanks, I would appreciate any help.
It looks like there are a few approaches. None of which may be ideal in your particular environment, but in the ActionView::Helpers, there is a url_for method that might get you what you want if you start using that. The other simple option is to use relative URLs (ie: '/controller/action' instead of 'https://myawesomesite.com/controller/action') which can be generated by the url_for method too.
Beyond that, it looks like you're getting in to work arounds, or digging in to the rails path generating code and altering it yourself.
Not probably the most ideal answer you were looking for, but there's my $0.02!
Take a look at http://ianlotinsky.wordpress.com/2010/09/29/ssl-in-ruby-on-rails/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With