【问题标题】:How to get ruby on rails production mount-point included in url_helpers for email?如何在电子邮件的 url_helpers 中获取 ruby​​ on rails 生产挂载点?
【发布时间】:2014-02-14 13:33:10
【问题描述】:

tl;dnr:我的页面链接很好,但链接到非页面文本(电子邮件)缺少生产应用程序挂载点。

详细信息:我的 RoR 应用在开发和生产中使用的 URL 略有不同:在开发中,顶级实体是资源模型:

http://localhost:3000/ENTITY/1/edit

但在生产环境中,应用安装在下一层,因此 URLS 有一个额外的术语:

https://server.example.com/APP/ENTITY/1/edit

这在大多数情况下都可以正常工作:像edit_entity_url 这样的 url_helpers 会返回我展示的 URL。坦率地说,我不清楚它到底是如何工作的。我想 Phusion Passenger 和 Rack 可以解决吗?

但是,当我发送电子邮件(例如确认密码重置)时,它不起作用:edit_entity_url 从不添加APP 挂载点。在 ERB 中缺少我自己的条件逻辑,我怎样才能在其中获得那个“/APP”?

我尝试过的事情:

  • 将 `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”令牌的地方):

<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的结构

【问题讨论】:

  • 现在我已经发布了,StackOverflow “类似问题” gremlin 指出了这个非常相似的、9 个月大的、未回答的问题:RackEnv not respected when using route helpers from model
  • 各种可搜索的讨论建议创建一个虚拟主机,而不是修改 URL,但这涉及 DNS 条目,这超出了我的直接范围。

标签: ruby-on-rails passenger rack


【解决方案1】:

嗯,这是一个恶心 hack,但我通过人工干预“解决”了这个问题。在app/views/user_mailer/password_reset.text.erb,我现在说:

To reset your password click the URL below.  

<%  url = root_url.chomp('/') %>
<%  url += "/registry" if Rails.env.production? %>
<%= url + edit_password_reset_path(@user.password_reset_token) %>

如果有人有更好的想法,仍然感兴趣!

【讨论】:

    【解决方案2】:

    您可以将 :script_name 参数传递给 url_for(或其他基于 url_for 的方法)来设置挂载点。我用

    url_for( :only_path => false,
    :host => some.host,
    :script_name => Rails.application.config.relative_url_root
    ...
    

    并在配置中设置 config.relative_url_root = '/your-mount-point'。

    关于它的更多细节在这里: how-to-get-ruby-on-rails-production-mount-point-included-in-url-helpers-for-email

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 2013-06-08
      • 2016-12-18
      相关资源
      最近更新 更多