Forum: Ruby on Rails SSL and url_for

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matt W. (Guest)
on 2006-04-28 18:11
(Received via mailing list)
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
Michael G. (Guest)
on 2006-04-28 18:20
(Received via mailing list)
Change "https" to "https://".

You can find more here:

http://www.michaelgorsuch.org/post/show/22
Robby R. (Guest)
on 2006-04-28 18:33
(Received via mailing list)
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. ;-)

redirect_to( :foo ) unless request.ssl?

-Robby


Robby R.
Founder & Executive Director

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

www.planetargon.com
www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]
Matt W. (Guest)
on 2006-04-28 18:54
(Received via mailing list)
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 ;)

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
Michael G. (Guest)
on 2006-04-28 19:09
(Received via mailing list)
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.
Matt W. (Guest)
on 2006-04-28 19:16
(Received via mailing list)
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
Robby R. (Guest)
on 2006-04-28 20:21
(Received via mailing list)
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... :-)

-Robby

Robby R.
Founder & Executive Director

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

www.planetargon.com
www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]
This topic is locked and can not be replied to.