如何在电子邮件的 url_helpers 中包含 ruby on rails 生产挂载点? [英] How to get ruby on rails production mount-point included in url_helpers for email?
问题描述
tl;dnr:我的页面链接没问题,但转换为非页面文本(电子邮件)的链接缺少生产应用挂载点.
tl;dnr: My page links are fine, but links erb-olated into non-page text (email) lack the production app mount-point.
detail:我的 RoR 应用在开发和生产中使用略有不同的 URL:在开发中,顶级实体是资源模型:
detail: My RoR app uses slightly different URLs in dev and production: in dev, the top-level entity is the resource model:
http://localhost:3000/ENTITY/1/edit
但在生产环境中,应用安装在低一级,因此 URLS 有一个额外的术语:
But in production, the app is mounted one level down, so URLS have an extra term:
https://server.example.com/APP/ENTITY/1/edit
这适用于大多数情况:像 edit_entity_url
这样的 url_helpers 返回我显示的 URL.坦率地说,我不清楚它是如何工作的.我想 Phusion 乘客和 Rack 能解决问题吗?
This works fine for most cases: url_helpers like edit_entity_url
return the URLs as I've shown them. Frankly, I'm not clear exactly how that works; I suppose Phusion Passenger and Rack work it out?
但是,当我发送电子邮件(例如确认密码重置)时,它不起作用:edit_entity_url
从不添加 APP
挂载点.在 ERB 中缺少我自己的条件逻辑,我怎样才能在那里获得/APP"?
However, when I send email (such as to confirm a password reset), it's not working: edit_entity_url
never adds the APP
mount-point. Short of my own conditional logic in ERB, how can I get that "/APP" in there?
我尝试过的事情:
- 设置 `Rails.application.routes.default_url_options[:host] 以包含/registry"(无效)
- 使用
<%= link_to 'Reset', edit_entity_url(...) %>
(无帮助)
版本信息:
- actionmailer (4.0.2)
- Rails 4.0.2
- 红宝石 2.1.0p0
- 机架 (1.5.2)
- 乘客 (4.0.35)
将事物联系在一起的 Apache启用站点"文件(也是我能找到的唯一定义了/registry"标记的地方):
The Apache "sites-enabled" file that ties things together (and the only place I can find that the "/registry" token is defined):
<VirtualHost *:80>
ServerName server.example.com
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Alias /registry /app01/capapp/registry/current/public
<Location /registry>
PassengerBaseURI /registry
PassengerAppRoot /app01/capapp/registry/current
</Location>
<Directory /app01/capapp/registry/current/public>
Allow from all
Options -MultiViews
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
Rails config/environments/production.rb
文件.
Registration::Application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
config.assets.version = '1.0'
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
Rails.application.routes.default_url_options[:host] = "server.example.com"
end
Registration::Application.config.middleware.use ExceptionNotification::Rack,
:email => {
:email_prefix => "[Registration]",
:sender_address => %{"Admin" <XXX@example.com>},
:exception_recipients => %w{XXX@example.com}
}
查看电子邮件(app/views/user_mailer/password_reset.text.erb
):
To reset your password click the URL below.
<%= edit_password_reset_url(@user.password_reset_token) %>
If you did not request your password to be reset please ignore this email
and your password will stay as it is.
是的,将 URL 基于 reset_token 而不是 user_id 是很奇怪的,但它是在控制器中处理的.基于 Railscast #274
Yes, it's odd to base the URL on the reset_token not the user_id, but it's handled in the controller. Structure based on Railscast #274
推荐答案
您可以将 :script_name 参数传递给 url_for(或其他基于 url_for 的方法)来设置挂载点.我用
You can pass the :script_name parameter to url_for (or other methods based on url_for) to set the mount point. I use
url_for( :only_path => false,
:host => some.host,
:script_name => Rails.application.config.relative_url_root
...
并在配置中设置 config.relative_url_root = '/your-mount-point'.
and set config.relative_url_root = '/your-mount-point' in the config.
关于它的更多细节在这里:how-to-get-ruby-on-rails-production-mount-point-included-in-url-helpers-for-email
More details about it here: how-to-get-ruby-on-rails-production-mount-point-included-in-url-helpers-for-email
这篇关于如何在电子邮件的 url_helpers 中包含 ruby on rails 生产挂载点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!