2015-02-09 41 views
0

我正在使用Rails 4,有Apartment Gem和Devise(3.4.1)的子域。由Devise Confirmable生成的電子郵件確認鏈接有問題。當用戶創建一個帳戶,他們收到一封電子郵件:無法確認子域名。使用Devise confirmmable和Rails 4

You can confirm your account email through the link below: 

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

但是,這是行不通的,因爲用戶的子域不鏈接,它應該是如

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

如果我在瀏覽器上手動添加子域鏈接,它確實確認該帳戶。

如何更改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-subdomainshttps://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' 
+0

太搞笑了!我有確切的*相反*問題。我有我的鏈接來顯示子域,但他們只是確認我是否刪除子域並提交。 – 2015-02-12 02:24:02

回答

2

我設法通過在confirmation_instructions.html.erb中使用以下內容來解決此問題。這適用於我的設置,因爲我使用公寓寶石:

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

Moikka Suomi!你的方法也解決了我的問題。我想,我們都有與公寓寶石類似的設置,所以...我的問題:我設置application_controller中的ActionMailer :: Base.default_url_options [:主機] =「#{subdomain} lvh.me:3000」'作爲before_filter函數;有沒有一種方法可以在confirmation_instructions.slim發送之前運行此控制器/操作? – 2016-05-23 11:51:13

0

你確認你的link_to,該:subdomain => @resource.subdomain部分居然有合法的價值?換句話說,你的「資源」(可能是用戶模型)有一個subdomain方法?