【发布时间】:2015-04-09 03:25:56
【问题描述】:
我正在使用 Rails 4、带有 Apartment gem 的子域和 Devise (3.4.1)。我对 Devise Confirmable 生成的电子邮件确认链接有疑问。当用户创建帐户时,他们会收到一封电子邮件:
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-subdomains 和 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'
【问题讨论】:
-
太搞笑了!我有确切的相反问题。我得到了显示子域的链接,但它们只能确认我是否删除子域并提交。
标签: ruby-on-rails ruby-on-rails-4 devise