English 中文(简体)
如何使自定义主机为设计邮件器工作?
原标题:How to make custom host working for devise mailer?

我与域名www.mysite.com有应用程序,该域名为Drupal和ap.mysite.com。

我有一个情况, 设计邮递员用www.mysite.com而不是ap.mysite.com指向www.mysite.com。

在设计.rb中,我将设置如下:

config.mailer = "Devise::Mailer"
ActionMailer::Base.default_url_options = { :host =>  app.mysite.com  }

生产.rb:

config.action_mailer.default_url_options = { :host =>  app.mysite.com  } 
ActionMailer::Base.smtp_settings = 
{
  :address   =>  smtp.sendgrid.net ,    # SendGrid SMTP server
  :domain    =>  mysite.com ,           # SendGrid account domain name 
  :user_name =>  username ,             # SendGrid user name
  :password  =>  password ,             # SendGrid password
  :enable_starttls_auto => true,
  :port => 587, 
  :authentication => :plain,
}

在view/devise/mailer/reset_password_ instructions.html.erb中,确认链接的外观如下:

<%= link_to  Change my password , edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %>

它在点击被遗忘的密码链接后成功发送电子邮件,并且对本地主机有吸引力,但是 URL 主机点指向www.mysite.com ,而不是app.mysite.com ,并投出404个错误。

我怎么能指向主机的右侧?

最佳回答

使用子域助手方法发送电子邮件的代码。 校对:Soup

module SubdomainHelper
  def with_subdomain(subdomain)
    subdomain = (subdomain || "")
    subdomain += "." unless subdomain.empty?
    host = Rails.application.config.action_mailer.default_url_options[:host]
    [subdomain, host].join
  end

  def url_for(options = nil)
   if options.kind_of?(Hash) && options.has_key?(:subdomain)
       options[:host] = with_subdomain(options.delete(:subdomain))
   end
   super
  end
end

帮助者的方法就是检查子域名 hash 是否存在。 如果存在的话, 方法在主机前先输入子域名, 并定义新的主机名默认主机名 。 我之所以没有经过这个过程, 是因为出于某种原因, 它没有在轨线3.2 上工作, 并且由于严格的时间限制, 我并不想再浪费时间去挖掘它。

我想,等等,我为什么不仅仅用邮递员的URL_方法来明确定义主机名称,而不是在所有这些方法中穿行。

因此,我所做的只是,在邮件机中定义了链接,用散列键 host 并传递了值作为我的子域。 之后它就像一个符咒: ) 。

在view/devise/mailer/reset_password_ instructions.html.erb中,确认链接的外观如下:

<%= link_to  Change my password , edit_password_url(@resource, :reset_password_token => @resource.reset_password_token, :host =>  app.mysite.com ) %>
问题回答

看来Defaise的默认行为是从主域发送的。

Here s a page that may help: https://github.com/plataformatec/devise/wiki/How-To:-Send-emails-from-subdomains





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签