简体   繁体   English

如何在Rails开发环境中使用主机生成URL?

[英]How to generate URL with host in Rails development environment?

I am working on an application that has set its default host to localhost : 我正在将其默认主机设置为localhost的应用程序上工作:

config.action_controller.default_url_options = { host: "localhost" }
config.action_mailer.default_url_options = { host: "localhost" }

This works fine if you're serving your app on a specific port (like http://localhost:3000 ). 如果您在特定端口(例如http:// localhost:3000 )上提供应用程序,则此方法很好。 I am using Pow so that I can access the app from a URL like http://myapp.dev . 我正在使用Pow,以便可以从类似http://myapp.dev的URL访问该应用程序。

How can I change this setting so that it will work with my domain as well as the other developers using localhost ? 如何更改此设置,以使其与我的域以及其他使用localhost开发人员一起使用? I need to generate full URLs since they will be used in emails. 我需要生成完整的URL,因为它们将在电子邮件中使用。 Is it possible to pass some sort of config value to Pow? 是否可以将某种配置值传递给Pow?

This is the best way I found: 这是我发现的最好方法:

1- Create a config/smtp_settings.example.yml file: 1-创建一个config / smtp_settings.example.yml文件:

development:
  :method: :letter_opener
  :url_options:
    :host: localhost
    :port: 3000

production: &production
  :method: :smtp
  :url_options:
    :host: www.the_site.com
  :smtp_settings:
    address: smtp.gateway.com
    port: 465
    user_name: someone@somewhere.com
    # etc ...

staging:
  <<: *production

test:
  :method: :test
  :url_options:
    :host: test.host

Notes: 笔记:

  • This uses letter_opener as development mailer, change to your own method 这使用letter_opener作为开发邮件,更改为您自己的方法
  • There is a staging environment defined as an exact copy of the production environment, remove if you don't use staging 有一个staging环境定义为production环境的精确副本,如果不使用staging ,则将其删除

2- Remove config.action_mailer.delivery_method and config.action_mailer.default_url_options and config.action_mailer.smtp_settings from all files under config/environments/ folder 2-从config / environments /文件夹下的所有文件中删除config.action_mailer.delivery_methodconfig.action_mailer.default_url_optionsconfig.action_mailer.smtp_settings

3- Add in the file config/application.rb the following: 3-在文件config / application.rb中添加以下内容:

config.action_mailer.delivery_method = config_for(:smtp_settings)[:method]
config.action_mailer.default_url_options = config_for(:smtp_settings)[:url_options]
config.action_mailer.smtp_settings = config_for(:smtp_settings)[:smtp_settings] if config_for(:smtp_settings)[:method] == :smtp

Notes: config_for has been introduced in the latest version of Rails (4.2.0 as per writing). 注意: config_for已在最新版本的Rails中引入(根据撰写,为4.2.0)。 If you use an older version of rails you must manually load the yml files 如果使用旧版本的rails,则必须手动加载yml文件


And the trick on top of all of that to answer your question: 所有这一切之上的伎俩来回答你的问题:

Push to your repository the file config/smtp_settings.example.yml and ignore the file config/smtp_settings.yml. 将文件config / smtp_settings.example.yml推送到您的存储库,并忽略文件config / smtp_settings.yml。

Each developper of your team would have to create their own config/smtp_settings.yml by copying the file smtp_settings.example.yml and changing the host to match their machine's IP address, so the mail send by each developer leads to their own machine. 团队中的每个开发人员都必须通过复制文件smtp_settings.example.yml并更改host以匹配其计算机的IP地址来创建自己的config / smtp_settings.yml,因此,每个开发人员发送的邮件都将引导至他们自己的计算机。

You off course requires you to start the local development server binded to 0.0.0.0 so it's accessible from other hosts (considering your security environment off course) 当然,您需要启动绑定到0.0.0.0的本地开发服务器,以便其他主机可以访问它(考虑到您的安全环境,当然)

In development, I like to use dotenv gem( https://github.com/bkeepers/dotenv ). 在开发中,我喜欢使用dotenv gem( https://github.com/bkeepers/dotenv )。

I put in .env : 我输入.env

DOMAIN=myapp.dev

And I put inside config/route.rb : MyApplication::Application.routes.draw do default_url_options host: ENV['DOMAIN'] 然后放入config/route.rbMyApplication::Application.routes.draw do default_url_options host: ENV['DOMAIN']

In production I define on heroku my domain with this command : 在生产中,我使用以下命令在heroku上定义我的域:

h config:set DOMAIN=myapp.herokuapp.com

or with a custom domain : 或使用自定义域:

h config:set DOMAIN=superdomain.com

This will check if Application root directory is symlinked in .pow of current user and set the host accordingly. 这将检查应用程序根目录是否在当前用户的.pow中被.pow链接,并相应地设置host

if File.exists?(Dir.home+"/.pow/#{Rails.root.basename}")
  config.action_mailer.default_url_options = { host: "#{Rails.root.basename}.dev" }
else
  config.action_mailer.default_url_options = { host: "localhost" }
end

You could use an environment variable to configure a default host. 您可以使用环境变量来配置默认主机。 See Luc Boissaye 's answer on how to set this up with dotenv . 请参阅Luc Boissaye关于如何使用dotenv进行设置的答案 If you don't add the .env to your repo, each developer can create his own .env and configure his (or her) own preferences. 如果您没有.env添加到您的.env中,则每个开发人员都可以创建自己的.env并配置自己(或她自己)的首选项。

But you can also use existing environment variables to configure the default_url_options . 但是,您也可以使用现有的环境变量来配置default_url_options For example Pow.cx sets the POW_DOMAINS variable: 例如,Pow.cx设置POW_DOMAINS变量:

config.action_mailer.default_url_options = {
  ENV['POW_DOMAINS'].present? ? 'my-app.dev' : 'localhost' 
}

As far as I know you only need to set the config.action_controller.default_url_options if you want to force it to some default. 据我所知,如果要强制将其设置为默认值,则只需设置config.action_controller.default_url_options When you use the rails _path helpers in your views, this will generate a relative URL ( /path ). 在视图中使用rails _path帮助器时,这将生成一个相对URL( /path )。 If you use the _url helpers, this will generate an absolute URL ( http://your.host/path ), but both the protocol ( http ) and host ( your.host ) are request based. 如果使用_url帮助器,这将生成一个绝对URL( http://your.host/path ),但是协议( http )和主机( your.host )都是基于请求的。

Described on Action Mailer Basics : Action Mailer基础上描述:

Unlike controllers, the mailer instance doesn't have any context about the incoming request so you'll need to provide the :host parameter yourself. 与控制器不同,邮件程序实例没有关于传入请求的任何上下文,因此您需要自己提供:host参数。

As the :host usually is consistent across the application you can configure it globally in config/application.rb : 由于:host通常在整个应用程序中是一致的,因此您可以在config/application.rb全局config/application.rb

config.action_mailer.default_url_options = { host: 'example.com' }

Because of this behavior you cannot use any of the *_path helpers inside of an email. 因此,您不能在电子邮件中使用任何* _path帮助器。 Instead you will need to use the associated *_url helper. 相反,您将需要使用关联的* _url帮助器。 For example instead of using 例如代替使用

<%= link_to 'welcome', welcome_path %>

You will need to use 您将需要使用

<%= link_to 'welcome', welcome_url %>

The use of Pow makes things a little trickier but thankfully it integrates with XIP.io so you can access myapp.dev from machines on the local network. 使用Pow使事情有些棘手,但值得庆幸的是,它与XIP.io集成在一起,因此您可以从本地网络上的计算机访问myapp.dev Just follow the instructions here: Accessing Virtual Hosts from Other Computers . 只需按照此处的说明进行操作: 从其他计算机访问虚拟主机

The question doesn't mention this, but it might be useful information - If you'd like to actually send mail in your dev (or perhaps test/staging) environment, Instead of seeing mail just in the rails console (with something like powder applog ), I'd recommend just hooking it up to a gmail . 这个问题没有提到,但是它可能是有用的信息-如果您想在开发(或测试/登台)环境中实际发送邮件,而不是仅在rails控制台中看到邮件(带有类似powder applog东西) powder applog ),建议您将其连接到gmail

You need to override default_url_options in your application controller (at least in rails 3) 您需要在应用程序控制器中覆盖default_url_options(至少在rails 3中)

http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options

class ApplicationController < ActionController::Base
 def default_url_options
  if Rails.env.production?
   {:host => "myproduction.com"}
  else  
   {}
  end
 end

end 结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM