nothing-special-here nothing-special-here - 2 months ago 6
HTTP Question

Rails 3, redirect from https to https, overwrite _path helper

I want to have mixed https/http site.

Moreover I want have redirects from https to http(ie. after user login successfully it should redirect to root page at http).

Gems like:


  • rack-ssl

  • rack-ssl-enforcer



works perfectly but only If you want to have entire site at https
"Mixed http/https" with only ssl at A, B, C actions and only http at D, E, F - dont work.

I checked solution from another SO thread:

Rails 3 SSL routing redirects from https to http

Almost works.
Its easy to write script which will change(on entire views) helper from "_path" to "_url".

But there is a problem with links like:

<%= link_to "model", some_model %>
<%= link_to "edit model", edit_mode_url(model) %>
...


There are many diffrent models and I use often "model" at iteration blocks, so solution based on 'rewrite' script will dont work with that.

Questions:

Is there a way to change behavior of
<%= link_to 'model', model %>
code to fix that? Is there a possibility to overwrite path helper(standard protocol will be http, on giver parameter - https)?

Or maybe there is a another solution which I have not found yet?

Edit:

I work with Rails 3.0.9.

Answer

Use this gem:

https://github.com/retr0h/ssl_requirement

gem install ssl_requirement

Then to add ssl_required :new, :destroy #others actions to your controllers.

If you use devise you have to overwrite each controller and specify all actions

devise_for :users, :controllers => { :confirmations => "confirmations", :omniauth_callbacks => "omniauth_callbacks", :passwords => "passwords",  :registrations => "registrations", :sessions => "sessions", :unlocks => "unlocks" } do
# etc
end

It works with Rails 3.0.x