SSL and url_for


#1

Hey, all!

I’m having a url problem that seems like I’m going to overcomplicate
it if I don’t just ask what I’m doing wrong…

My situation is that I’m testing to see if a visitor is accessing a
controller via SSL, and if not, redirect to the same controller via my
secure address. This really only amounts to replacing http with https,
and so I did the following:

redirect_to(:protocol => “https”) if !request.ssl?

I’m doing this via localhost for now, so I’m not expecting it to
actually work. However, I’m getting an incorrect url when I try this:

http://localhost:3000httpslocalhost:3000/cart/checkout

When I log the url it’s creating with url_for, it’s creating:
httpslocalhost:3000/cart/checkout. This seems incorrect… What am I
doing wrong? I’ve set only_path to false, but that seems to have no
effect.

My alternative would be to parse the url and do the replacement and
redirect myself, but this seems like it should work! What am I doing
wrong?

Thanks!

Matt


#2

Change “https” to “https://”.

You can find more here:

http://www.michaelgorsuch.org/post/show/22


#3

On Apr 28, 2006, at 7:09 AM, Matt W. wrote:

redirect_to(:protocol => “https”) if !request.ssl?

Not sure about your error… but I wanted to comment on this line.

if !some.cond?

could be written as:

unless some.cond?

It’s easier on the eyes. :wink:

redirect_to( :foo ) unless request.ssl?

-Robby

Robby R.
Founder & Executive Director

PLANET ARGON, LLC
Ruby on Rails Development, Consulting & Hosting


www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]


#4

Thanks to both of you! The docs were a little unclear on it, because the
example showed:

proto://host:port/:controller/:action

I thought that the :// would be implied in any request, so it seemed odd
that I would need to add it. Just didn’t want hack it :wink:

And thanks for the syntax tip, Robby. I’m still trying to learn the
“ruby
way”, and it always helps to get little tips like that!

Matt


#5

Thanks for your blog post, as well… That’s a nice DRY way to do it.
I’m going to use a similar solution.

Matt


#6

On Apr 28, 2006, at 7:45 AM, Matt W. wrote:

And thanks for the syntax tip, Robby. I’m still trying to learn the
“ruby way”, and it always helps to get little tips like that!

That’s what I figured… :slight_smile:

-Robby

Robby R.
Founder & Executive Director

PLANET ARGON, LLC
Ruby on Rails Development, Consulting & Hosting


www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]


#7

Matt - I ran into the same problem when I was first working on SSL.

The docs make you think the “//” is implied.

Glad to see it’s working.