开发者

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

开发者 https://www.devze.com 2023-04-08 02:18 出处:网络
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).

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.


If you would like to add https to a particular route

you can use this code

before_filter :redirect_to_https
def redirect_to_https
     redirect_to :protocol => "https://" unless (request.ssl? || request.local?)
end

You can define the routes you would like to use with the before_filter action simply by doing the following

before_filter :redirect_to_https, :except => [:action1 , :action2]
before_filter :redirect_to_https, :only => [:action1 , :action2]


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号