Amitkumar Jha
Amitkumar Jha

Reputation: 1333

How to Force SSL to Specific Named Route in Rails

I have a Rails Application in which I have used force_ssl method as

def force_ssl # to use https for payment page
  if !Rails.env.development? && !request.ssl?
    redirect_to :protocol => 'https'
  end
end

for a Particular action name 'abc' with named route

match '/find-a-abc' => "home#abc"

when I go to URL

http://local.demo.com/find-a-abc

it will redirect_to https://local.demo.com/abc

Any Solution? so it will redirect_to Particular route, rather than redirecting to an Action when using a https Protocol.

Upvotes: 0

Views: 670

Answers (1)

benjaminjosephw
benjaminjosephw

Reputation: 4417

It doesn't look like you've provided a named route here (i.e. match '/find-a-abc' => "home#abc", :as => :named_route). You will need to do this and call named_route_url rather than just the controller and action to get the right URL.

If you want a specific route to always be handled with SSL, you could define the route like so:

scope :protocol => 'https://', :constraints => { :protocol => 'https://' } do
  match '/find-a-abc' => "home#abc", :as => :abc
end

Then abc_url should always be "https://local.demo.com/find-a-abc"

Upvotes: 1

Related Questions