如何从生成的 URL 中删除端口? rails

How can I remove the port from generated URLs? rails

我遇到一个问题,我的应用程序从邮件程序生成 links 到 www.myurl.herokuapp.com/:8080/someurl - 这破坏了我所有的 links,因为它们可以工作,但不会添加 :8080。即如果我手动删除 /:8080/ 它工作正常。

例如,通过电子邮件生成的 url 重置密码是:

https://myurl.herokuapp.com/:8080/users/password/edit?reset_password_token=123token123

然而,它在开发中运行良好,只是在生产中出现了问题。我已经设法通过对某些 URL 使用静态 urls 来破解它,但为了密码重置和长期健康,我需要修复它。

我正在使用: Ruby 2.3.1 Rails5.1.4 Heroku PGSQL SendInBlue 作为邮件程序

我尝试过的:

在reset_password_instructions.html.erb中:

更改行自:

<p><%= link_to 'Change my password', edit_password_path(@resource, reset_password_token: @token) %></p>

我还尝试从 production.rb 中删除默认端口:

config.action_mailer.default_url_options = { 主机:'http://myurl.herokuapp.com/' }

任何人都可以建议可能导致此问题的原因或解决方法吗?

错误信息:

The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.

If you are the application owner check the logs for more information.

单击电子邮件中的 link 时,日志似乎没有抛出任何新内容,实际上它们不会生成任何内容(?),日志中的最后一件事:

2018-09-25T21:16:06.581471+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2018-09-25T21:16:06.709308+00:00 heroku[web.1]: Process exited with status 143
2018-09-25T21:27:39.622506+00:00 heroku[router]: at=info method=GET path="/szallodak/4909;;;" host=staging.herokuapp.com request_id=0eb96201-d562-46d2-93aa-759324d0c64d fwd="95.216.157.196" dyno=web.1 connect=0ms service=6ms status=301 bytes=170 protocol=http
2018-09-25T21:27:41.361516+00:00 heroku[router]: at=info method=GET path="/szallodak/4909" host=staging.herokuapp.com request_id=cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677 fwd="95.216.157.196" dyno=web.1 connect=0ms service=132ms status=404 bytes=1966 protocol=https
2018-09-25T21:27:41.227565+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] Started GET "/szallodak/4909" for 95.216.157.196 at 2018-09-25 21:27:41 +0000
2018-09-25T21:27:41.355281+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677]
2018-09-25T21:27:41.355383+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] ActionController::RoutingError (No route matches [GET] "/szallodak/4909"):
2018-09-25T21:27:41.355437+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677]
2018-09-25T21:27:41.355533+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
2018-09-25T21:27:41.355535+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:36:in `call_app'
2018-09-25T21:27:41.355530+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/debug_exceptions.rb:63:in `call'
2018-09-25T21:27:41.355537+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `block in call'
2018-09-25T21:27:41.355539+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:69:in `block in tagged'
2018-09-25T21:27:41.355540+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:26:in `tagged'
2018-09-25T21:27:41.355542+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:69:in `tagged'
2018-09-25T21:27:41.355544+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `call'
2018-09-25T21:27:41.355545+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
2018-09-25T21:27:41.355547+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/request_id.rb:25:in `call'
2018-09-25T21:27:41.355548+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
2018-09-25T21:27:41.355550+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
2018-09-25T21:27:41.355552+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/activesupport-5.1.6/lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
2018-09-25T21:27:41.355554+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/executor.rb:12:in `call'
2018-09-25T21:27:41.355555+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/static.rb:125:in `call'
2018-09-25T21:27:41.355557+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
2018-09-25T21:27:41.355558+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/ssl.rb:68:in `call'
2018-09-25T21:27:41.355560+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/railties-5.1.6/lib/rails/engine.rb:522:in `call'
2018-09-25T21:27:41.355561+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/configuration.rb:225:in `call'
2018-09-25T21:27:41.355563+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:658:in `handle_request'
2018-09-25T21:27:41.355564+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:472:in `process_client'
2018-09-25T21:27:41.355566+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:332:in `block in run'
2018-09-25T21:27:41.355568+00:00 app[web.1]: [cdc1c6e4-ce66-404c-a4a7-ed8a7c7eb677] vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/thread_pool.rb:133:in `block in spawn_thread'
2018-09-25T21:27:41.657427+00:00 heroku[router]: at=info method=GET path="/szallodak/4909;;;" host=staging.herokuapp.com request_id=47227af4-6a27-4265-8f9f-357d89889a92 fwd="95.216.157.196" dyno=web.1 connect=0ms service=3ms status=301 bytes=170 protocol=http

对于其他有相同问题的人来说非常容易修复,在 production.rb 中我只是添加了:“,协议:'https'”所以它现在显示为:

config.action_mailer.default_url_options = { host: 'http://myapp.herokuapp.com',
     protocol: 'https' }

如果您的协议不是 https,那么您只需使用 http://

我还恢复了(虽然我没有使用路径测试它)路径,两者都应该有效:

<p><%= link_to 'Change my password', edit_password_path(@resource, reset_password_token: @token) %></p>

<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>