연결할 호스트가 없습니다! : host 매개 변수를 제공하거나 default_url_options [: host]를 설정하십시오
나는 지금 약 90 분 동안 인터넷 검색을 해왔으며 여전히 이것에 대한 대답이 없습니다. 어디에서 설정 default_url_options
합니까? 이미 config.action_mailer.default_url_options
다른 곳 에서이 동일한 버그를 해결하기 위해 설정 했지만 RSpec 사양 내에서 URL 도우미를 사용하려고 할 때이 오류가 발생합니다. 어디에서 default_url_options가 설정 될지 알 수 없습니다.
Failure/Error: listing_url(listing).should match(/\/\d+-\w+$/)
RuntimeError:
Missing host to link to! Please provide :host parameter or set default_url_options[:host]
# ./spec/routing/listing_routing_spec.rb:9:in `block (3 levels) in <top (required)>'
이 코드는 이메일 / ActionMailer와 아무 관련이 없으며 경로 대신 URL이 필요합니다.
어떤 아이디어?
모든 환경에서 다음 줄을 추가해야합니다.
config.action_mailer.default_url_options = { :host => "yourhost" }
그렇게하면 모든 환경에서 작동 할 수 있으며 환경마다 다를 수 있습니다. 예를 들면 다음과 같습니다.
development.rb
config.action_mailer.default_url_options = { :host => "dev.yourhost.com" }
test.rb
config.action_mailer.default_url_options = { :host => "test.yourhost.com" }
production.rb
config.action_mailer.default_url_options = { :host => "www.yourhost.com" }
Your::Application.routes.draw do
default_url_options :host => "example.com"
# ... snip ...
end
어딘가에 routes.rb
:)
호스트는 각 환경의 구성 파일에서 지정해야합니다. 예 :
config/environments/development.rb
default_url_options
을 사용하도록 설정 하십시오 action_mailer.default_url_options
.
환경 파일 (예를 들어 각에서 development.rb
, production.rb
등) 당신은을 지정할 수 있습니다 default_url_options
에 대한 사용 action_mailer
:
config.action_mailer.default_url_options = { host: 'lvh.me', port: '3000' }
그러나 이들은 다음에 대해 설정되지 않았습니다 MyApp:Application.default_url_options
.
$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}
$ MyApp::Application.default_url_options
#=> {}
그래서 외부에서 오류가 발생하는 이유입니다 ActionMailer
.
당신은 당신의 응용 프로그램입니다 설정할 수 있습니다 default_url_options
당신을 위해 정의 된 것을 사용하는 것이 action_mailer
적절한 환경 파일 (에 development.rb
, production.rb
등).
가능한 한 건조한 것들을 유지하려면 config/environment.rb
파일 에서이 작업을 수행하면 한 번만 수행하면됩니다.
# Initialize the rails application
MyApp::Application.initialize!
# Set the default host and port to be the same as Action Mailer.
MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options
이제 앱을 부팅하면 전체 애플리케이션 default_url_options
이 다음과 일치합니다 action_mailer.default_url_options
.
$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}
$ MyApp::Application.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}
이 길로 나를 안내해 준 @pduersteler의 모자 팁 .
어떤 listing_url
방법 을 사용 하면 전체 URL이 반환됩니다 (상대적으로는 아닙니다). 레일즈가 전체 URL을 계산하기 위해 호스트를 요구하는 이유입니다.
어떻게 호스트에게 레일을 말할 수 있습니까? 여러 가지 방법으로 수행 할 수 있습니다.
1. 각 환경에이 옵션 추가 :
[/config/development.rb]
config.action_mailer.default_url_options = { host: "localhost:3000" }
[/config/test.rb]
config.action_mailer.default_url_options = { host: "localhost:3000" }
[/config/production.rb]
config.action_mailer.default_url_options = { host: "www.example.com" }
참고 : 레일 엔진 내부에서 작업하는 경우 엔진 테스트 내부의 더미 앱에 대해서도 동일한 작업을 수행해야합니다. 엔진을 테스트 path_to_your_engine/test/dummy/config/environments/*
할 때는 레일이 테스트하는 것이기 때문입니다.
다음과 같이 foo_url 메소드에 호스트 옵션을 추가하십시오.
listing_url(listing, host: request.host) # => 'http://localhost:3000/listings/1'
3. 옵션으로 호스트를 출력하지 않습니다:only_path to true
.
listing_url(listing, only_path: true ) # => '/listings/1'
IMHO I don't see the point on this one because in this case I would use the listing_path
method
Funny thing, that setting config.action_mailer.default_url_options
does not help for me. Also, messing around with environment-independent settings in places I felt like it does not belong was not satisfying for me. Additionally, I wanted a solution that worked when generating urls in sidekiq/resque workers.
My approach so far, which goes into config/environments/{development, production}.rb
:
MyApp::Application.configure do
# Stuff omitted...
config.action_mailer.default_url_options = {
# Set things here as usual
}
end
MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options
This works for me in rails >= 3.2.x.
You can always pass host as a parameter to the URL helper:
listing_url(listing, host: request.host)
Rails.application.routes.default_url_options[:host]= 'localhost:3000'
In the developemnt.rb / test.rb, can be more concise as following:
Rails.application.configure do
# ... other config ...
routes.default_url_options[:host] = 'localhost:3000'
end
You can set default url options in the Application Controller:
class ApplicationController < ActionController::Base
def default_url_options
{:locale => I18n.locale}
end
end
http://guides.rubyonrails.org/action_controller_overview.html#default_url_options
I had this same error. I had everything written in correctly, including the Listing 10.13 from the tutorial.
Rails.application.configure do
.
.
.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delevery_method :test
host = 'example.com'
config.action_mailer.default_url_options = { host: host }
.
.
.
end
obviously with "example.com" replaced with my server url.
What I had glossed over in the tutorial was this line:
After restarting the development server to activate the configuration...
So the answer for me was to turn the server off and back on again.
go to config/environments/test.rb
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
Adding the default_url in routes not the right solution although, it works for some cases.
You've to set the default_url in each environment(development, test, production).
You need make these changes.
config/environments/development.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
config/environments/test.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
config/environments/development.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
'IT' 카테고리의 다른 글
git-worktree를 무엇에 사용합니까? (0) | 2020.05.26 |
---|---|
Django : 일부 모델 필드가 서로 충돌하는 이유는 무엇입니까? (0) | 2020.05.26 |
앱의 푸시 알림 설정 재설정 (0) | 2020.05.25 |
배열의 한 속성에서 알파벳 순서로 배열의 객체 정렬 (0) | 2020.05.25 |
팬더 기능을 열에 적용하여 여러 개의 새 열을 작성 하시겠습니까? (0) | 2020.05.25 |