无法确认子域的帐户。使用 Devise 可确认和 Rails 4

Can't confirm account for subdomain. Using Devise confirmable and Rails 4

我正在使用 Rails 4,带有 Apartment gem 和 Devise (3.4.1) 的子域。我对 Devise Confirmable 生成的电子邮件确认 link 有疑问。当用户创建帐户时,他们会收到一封电子邮件:

You can confirm your account email through the link below:

http://lvh.me:3000/users/confirmation?confirmation_token=xsTNUw5oQYTPf_CBwZXD

但是,它不起作用,因为用户的子域不在 link 上,例如应该是

http://mysubdomain.lvh.me:3000/users/confirmation?confirmation_token=xsTNUw5oQYTPf_CBwZXD

如果我在浏览器上手动添加子域 link,它会确认帐户。

如何更改 confirmation_instructions.html.erb link_to 以生成一个 confirmation_url 并在其前面添加子域? 例如:

<p>Welcome <%= @email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token, :subdomain => @resource.subdomain) %></p>

我尝试过,例如https://github.com/plataformatec/devise/wiki/How-To:-Send-emails-from-subdomains and https://github.com/fortuity/rails3-subdomain-devise/wiki/Tutorial-%28Walkthrough%29 但两者都不适合我,我得到了一个错误:

 "NoMethodError in Accounts#create, undefined method `subdomain' for #<User:0x...>"

这是我的 AccountsController 中的创建操作:

def create
  @account = Account.new(account_params)
    if @account.valid? && verify_recaptcha
       Apartment::Tenant.create(@account.subdomain)
       Apartment::Tenant.switch(@account.subdomain)
       @account.save
       @account.create_default_lists
       redirect_to new_user_session_url(subdomain: @account.subdomain)
    else
       render action: 'new'
    end
end

这是我的路线:

user_confirmation     POST   /users/confirmation(.:format)                         devise/confirmations#create
new_user_confirmation GET    /users/confirmation/new(.:format)                   devise/confirmations#new
                      GET    /users/confirmation(.:format)                       devise/confirmations#show

在development.rb中我设置了:

config.action_mailer.default_url_options = { host: "lvh.me:3000" }

在 ApplicationController 中我有:

protect_from_forgery
before_filter :authenticate_user!, :set_mailer_host

private

def current_account
   @current_account ||= Account.find_by(subdomain: request.subdomain)
end

helper_method :current_account

def set_mailer_host
   subdomain = current_account ? "#{current_account.subdomain}." : ""
   ActionMailer::Base.default_url_options[:host] = "#{subdomain}lvh.me:3000"
end

end

这是控制台上的错误:

ActionView::Template::Error (undefined method `subdomain' for #<User:0x007fbfcc27e5a0>):
    2: 
    3: <p>You can confirm your account email through the link below:</p>
    4: 
    5: <p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token, **:subdomain => @resource.subdomain**) %></p>
  app/views/devise/mailer/confirmation_instructions.html.erb:5:in `_app_views_devise_mailer_confirmation_instructions_html_erb___2999600516167812245_702'
  app/controllers/accounts_controller.rb:14:in `create'

您是否确认 link_to 中的 :subdomain => @resource.subdomain 部分确实具有合法值?换句话说,你的 "resource" (可能是用户模型)有一个 subdomain 方法?

我设法通过使用 confirmation_instructions.html.erb 中的以下方法解决了这个问题。这适用于我的设置,因为我使用的是公寓 gem:

<%= link_to 'Confirm my account', user_confirmation_url(confirmation_token: @token, subdomain: Apartment::Tenant.current) %>