2012-02-28 3 views
9

레일스 3.1+ URL 도우미가 호출하는 모든 도우미에서 URL 도우미를 https 프로토콜을 사용하지 않고도 사용하고 싶습니다. 모든 레일 헬퍼에서 프로토콜을 https로 변경하십시오.

ROUTES_PROTOCOL = (ENV["RAILS_ENV"] =~ /development/ ? 'http://' : 'https://') 

scope :protocol => ROUTES_PROTOCOL, :path => "/app" do 

방법이 작업을 수행 할 수 있습니다 주위에 내가 예를 들어 여러 가지 있지만 없음 작업을 찾았 검색 한 후? 단지 config/environments에 설정 파일이 설정 라인을 추가 : SSL (// HTTPS) : 사용하고자하는 어느 환경

답변

0

YOURAPPNAME::Application.configure do 

    # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 
    config.force_ssl = true 
end 
+2

요청 프로토콜이 http 일 때 리디렉션이 발생하지만 주로 이메일의 링크 인 경우 URL 도우미에서 https를 사용합니다. – 99miles

1

을 당신이 당신의 응용 프로그램에 SSL을 강제하려는 경우를이 될 수 있습니다 application.rb (또는 환경 별 파일)에서 config.force_ssl을 true로 설정하면됩니다. 주제 here

편집 확인에 대한 자세한 그래서 이것에 대한 충분한 증거를 찾을 수없는,하지만 난 당신이 응용 프로그램 컨트롤러에 default_url_options=(options={})를 오버라이드 (override) 할 수 있다고 생각하고, 함수 본문에 :protocol => :https을 설정합니다. 이메일에 대한 트릭이 아니라면 config.action_mailer.default_url_options을 추가하여 환경 설정에서이 절차를 반복해야합니다. 희망이 그것을 않습니다! force_ssl이 참 기본적으로 레일 3.2.1에서

+1

요청 프로토콜이 http 일 때 리디렉션이 발생하지만 주로 이메일의 링크 인 경우 URL 도우미에서 https를 사용합니다. – 99miles

0

, = 사실이

  1. 열기 설정/환경/production.rb을 확인하고 "config.force_ssl"

config.force_ssl를 검색 할 수 - 변경할 필요가 없습니다.

config/environments/development.rb - 이제 로컬로 실행되는 서버이기 때문에 config.force_ssl을 배치 할 필요가 없습니다..

가 가 여기

if !request.ssl? 
    "https://" + request.host + request.request_uri 
elsif request.ssl? 
    "http://" + request.host + request.request_uri 
end 

위의 경우 다른 사람에와 ActionView::Helpers에서 도우미베이스에 데프 추가 다른보기 좋아이다

, 당신이 것을 사용하기 시작하면 당신이 원하는 걸 얻을 수있는 url_for 방법이있다 .

+1

요청 프로토콜이 http 일 때 리디렉션이 발생하지만 주로 이메일의 링크 인 경우 URL 도우미에서 https를 사용합니다. – 99miles

+0

@ 99miles - 답변을 변경했습니다. 확인해주세요. 문제가 해결되면 알려주세요. –

12

이메일의 링크를 주로 원하십니까?

내 생각에 이것은 production.rb, development.rb 또는 다른 환경에서 작동 할 것입니다.

config.action_mailer.default_url_options = { 
    :host => 'yourwebsite.com', 
    :protocol => 'https' 
} 

# Makes it possible to use image_tag in mails 
config.action_mailer.asset_host = "https://yourwebsite.com" 
12

레일 4를 사용하는 경우 ApplicationController#default_url_options을 정의하면 작동하지 않습니다.URL 옵션은 이제 응용 프로그램의 루트의 설정에 정의되어 있습니다

Rails.application.routes.draw do 
    default_url_options protocol: :https 
end 
+10

다음 파일을 내 production.rb 파일에 저장했습니다. 'Rails.application.routes.default_url_options [: protocol] = 'https'' – gitb

+0

Rails 5.1.4 용 콘솔에서만 작동합니다. – lulalala

0

에 확인하실 수 있습니다 ApplicationController

def default_url_options(options={}) 
    options.merge(protocol: :https) 
    end 

이 코드를 추가 할 수 있습니다 이 작품은 나를 위해 :

config/environments/production.rb

Rails.application.routes.default_url_options[:protocol] = 'https' 

루비 2.1.4p265은 (2014년 10월 27일 개정 48,166) x86_64에 리눅스]는 레일 5.1.4 3.2.22.5

0

는 I가 다음과 같은 시나리오를 테스트 한 레일 :

# in development.rb 
config.action_controller.default_url_options({:protocol => 'https'}) 
config.action_controller.default_url_options(:protocol => 'https') 
# Does not work 

# in development.rb, outside config block 
Rails.application.routes.default_url_options[:protocol] = 'https' 
# Does not work, but works under console 

# in routes.rb 
Rails.application.routes.draw do 
    default_url_options protocol: :https 
# Does not work, but works under console 

# in ApplicationController 
def default_url_options(options={}) 
    { secure: true } 
end 
# Does not work 

# in ApplicationController 
def default_url_options 
    { protocol: :https } 
end 
# Works in browser, but does not work under console 

# in development.rb 
config.action_controller.default_url_options= {:protocol => 'https'} 
# Works in browser, but does not work under console 
관련 문제