如何根据环境设置 config.action_controller.default_url_options = {:host = '#''}

发布于 2024-11-02 04:32:48 字数 1569 浏览 3 评论 0原文

现在我正在使用适用于开发主机的这个,但我必须手动更改 {:host =>;当我转向生产时“”}代码。

post.rb

def share_all
  url =  Rails.application.routes.url_helpers.post_url(self, :host => 'localhost:3000')
  if user.authentications.where(:provider => 'twitter').any?
    user.twitter_share(url)  
  end
end

我想使用它,然后定义每个环境的 default_url_options:

post.rb

def share_all
  url =  Rails.application.routes.url_helpers.post_url(self)
  if user.authentications.where(:provider => 'twitter').any?
    user.twitter_share(url)  
  end
end

我尝试将其添加到我的 config/environments/development.rb 但我仍然得到“缺少要链接的主机!请提供:host 参数或设置 default_url_options[:host]" 错误

development.rb

config.action_controller.default_url_options = {:host => "localhost:3000"}

我什至尝试过这种方式:

development.rb

config.action_controller.default_url_options = {:host => "localhost", :port => "3000"}

编辑:

我现在也遵循了这个,但仍然是相同的错误指南 http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options

应用程序控制器

class ApplicationController < ActionController::Base
  protect_from_forgery
  include ApplicationHelper
  def default_url_options
    if Rails.env.production?
      { :host => "example.com"}
    else
      {:host => "example1.com"}
    end
  end
end

这让我发疯,我在这里错过了什么???

Right now I'm using this which works for the development host, but I have to manually change the {:host => ""} code when I move to production.

post.rb

def share_all
  url =  Rails.application.routes.url_helpers.post_url(self, :host => 'localhost:3000')
  if user.authentications.where(:provider => 'twitter').any?
    user.twitter_share(url)  
  end
end

I'd like to use this and then define the default_url_options per environment:

post.rb

def share_all
  url =  Rails.application.routes.url_helpers.post_url(self)
  if user.authentications.where(:provider => 'twitter').any?
    user.twitter_share(url)  
  end
end

I've tried adding this to my config/environments/development.rb but I still get the "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" error

development.rb

config.action_controller.default_url_options = {:host => "localhost:3000"}

And I even tried it this way:

development.rb

config.action_controller.default_url_options = {:host => "localhost", :port => "3000"}

EDIT:

I've now also followed this and still the same error guide http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options

application controller

class ApplicationController < ActionController::Base
  protect_from_forgery
  include ApplicationHelper
  def default_url_options
    if Rails.env.production?
      { :host => "example.com"}
    else
      {:host => "example1.com"}
    end
  end
end

This is driving me crazy, what am I missing here???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

白龙吟 2024-11-09 04:32:49

我知道这是一个旧线程,但我在 Ruby 2.6.3 和 Rails 5.2.3 中遇到了这个问题。我看到的行为基本上是我添加的每个路径都会失败,并出现 Error during failuresafe response: undefined method 'empty?'对于 nil:NilClass。在生产中它工作得很好,但在我的开发环境中,我会收到上面提到的错误。


对我来说,修复方法是将其添加到controllers/application_controller.rb

def default_url_options
  if Rails.env.production?
    Rails.application.routes.default_url_options = { host: "www.production-domain.com", protocol: 'https' }
  elsif Rails.env.development?
    Rails.application.routes.default_url_options = { host: 'localhost:3000', protocol: 'http' }
  end
end

然后我就可以在本地运行我的开发环境。

I know this is an old thread, but I ran into this with Ruby 2.6.3 and Rails 5.2.3. The behavior I was seeing was basically that every path I added would fail with Error during failsafe response: undefined method 'empty?' for nil:NilClass. In production it worked fine, but in my development environment, I would get the error mentioned above.


The fix for me was add this to controllers/application_controller.rb:

def default_url_options
  if Rails.env.production?
    Rails.application.routes.default_url_options = { host: "www.production-domain.com", protocol: 'https' }
  elsif Rails.env.development?
    Rails.application.routes.default_url_options = { host: 'localhost:3000', protocol: 'http' }
  end
end

I was then able to run my development environment on my local.

一杯敬自由 2024-11-09 04:32:49

我自己尝试在 rake 任务中生成 URL 时遇到了这个问题。这绝对是不明显的,而且其中许多解决方案都会起作用。我的解决方案是从 @joshua-pinter 起飞,但在环境文件中完成。例如,我的 development.rb 如下所示:

Rails.application.configure do
  …
  config.action_mailer.default_url_options = self.default_url_options = { host: 'localhost', port: 3000 }
  …
end

development.rb 进行了适当的更改。

Just ran into this problem myself trying to generate a URL in a rake task. It's definitely non-obvious and many of these solutions will work. My solution is a take off from @joshua-pinter's but done in the environment file. For example my development.rb looks like:

Rails.application.configure do
  …
  config.action_mailer.default_url_options = self.default_url_options = { host: 'localhost', port: 3000 }
  …
end

with the appropriate changes made to production.rb.

甜警司 2024-11-09 04:32:49

对我来说有效的是

ActionMailer::Base.default_url_options = { host: "yourhosthere"} # e.g. yourhosthere=localhost:3000 or yourhosthere=example.com

,如果您已经在配置文件中设置了端口,那么仅更改 [:host] 就会导致错误

TypeError (no implicit conversion of Symbol into Integer):

For me whats worked is

ActionMailer::Base.default_url_options = { host: "yourhosthere"} # e.g. yourhosthere=localhost:3000 or yourhosthere=example.com

because if you already set a port in the config files then changing the [:host] only will result in an error

TypeError (no implicit conversion of Symbol into Integer):
风筝在阴天搁浅。 2024-11-09 04:32:49

在每个环境文件中设置特定的 url 选项

config.action_mailer.default_url_options = { host: 'hostname', port: 3000 }

,然后在 config/routes.rb 文件中设置默认 url 选项

Rails.application.routes.draw do
  default_url_options Rails.application.config.action_mailer.default_url_options
  ...
end

Set your specific url options in each environment file with

config.action_mailer.default_url_options = { host: 'hostname', port: 3000 }

And then set the default url options in your config/routes.rb file with

Rails.application.routes.draw do
  default_url_options Rails.application.config.action_mailer.default_url_options
  ...
end
亢潮 2024-11-09 04:32:48

好吧,我发现正确的写法是

Rails.application.routes.default_url_options[:host] = 'localhost:3000'

:)

Okay I figured it out the correct way to write it is

Rails.application.routes.default_url_options[:host] = 'localhost:3000'

:)

简单气质女生网名 2024-11-09 04:32:48

ActionMailer 继承应用程序的 default_url_options

您希望尽可能保持干燥,因此,理想情况下,您不想在同一环境的多个位置对主机和端口进行硬编码,除非您的 ActionMailer 实际上 使用与应用程序的其余部分不同的主机和端口。

要为整个 Application 设置 default_url_options,只需将以下行添加到 config/environment.rb 文件中(更改 MyApp< /code> 到您应用的名称):

# 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

这将解决您的问题并自动将您的 Applicationdefault_url_options 设置为与您的相同config.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"}

Inherit your Application's default_url_options from ActionMailer.

You want to keep things as DRY as possible so, ideally, you don't want to hard code your host and port in multiple places for the same environment, unless your ActionMailer actually uses a different host and port than the rest of your Application.

To set the default_url_options for your entire Application, simply add the following line to your config/environment.rb file (changing MyApp to your app's name):

# 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

This will fix your problem and automatically set your Application's default_url_options to the same as your config.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"}
清醇 2024-11-09 04:32:48

您必须重新启动服务器才能使此文件的更改生效。

You have to restart your server before the changes to this file takes effect.

汹涌人海 2024-11-09 04:32:48

config/environments/development.rb(任何其他环境,相同)

添加此行以及您想要的主机

routes.default_url_options[:host] = 'localhost:3000'

config/environments/development.rb (any other environment, same)

add this row with host that you want

routes.default_url_options[:host] = 'localhost:3000'
丶视觉 2024-11-09 04:32:48

config.action_mailer.default_url_options = { :host =>; "your host" }

例如您的主机 localhost:3000

您可以将其放入 test.rb、development.rb、 Production.rb 文件中 主机可能因环境而异

config.action_mailer.default_url_options = { :host => "your host" }

for instance your host localhost:3000

you can put this in test.rb, development.rb, production.rb files host could be different from environment to environment

雄赳赳气昂昂 2024-11-09 04:32:48

我通常是这样解决这个问题的:

首先,从 rails new 自动生成的文件中正确设置您的 config/environments/Production.rb (和其他环境):

Rails.application.configure do
  # [...]
  config.action_mailer.default_url_options = {
    host: "https://your_app.your_domain.tld"
  } # You may also use an env variable ENV['HOST'] if necessary
end

然后,返回到您的 config/application.rb 并设置为从此 ActionMailer 配置继承:

require_relative "boot"
require "rails/all"
Bundler.require(*Rails.groups)

module YourModule
  class Application < Rails::Application
    # [...]
    config.after_initialize do |app|
      app.routes.default_url_options = app.config.action_mailer.default_url_options
    end
  end
end

This is how I usually go about this problem:

First, properly set your config/environments/production.rb (and other environments) from the rails new auto-generated file:

Rails.application.configure do
  # [...]
  config.action_mailer.default_url_options = {
    host: "https://your_app.your_domain.tld"
  } # You may also use an env variable ENV['HOST'] if necessary
end

Then, go back to your config/application.rb and set to inherit from this ActionMailer configuration:

require_relative "boot"
require "rails/all"
Bundler.require(*Rails.groups)

module YourModule
  class Application < Rails::Application
    # [...]
    config.after_initialize do |app|
      app.routes.default_url_options = app.config.action_mailer.default_url_options
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文