【问题标题】:Can't confirm account for subdomain. Using Devise confirmable and Rails 4无法确认子域的帐户。使用 Devise 可确认和 Rails 4
【发布时间】: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-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'

【问题讨论】:

  • 太搞笑了!我有确切的相反问题。我得到了显示子域的链接,但它们只能确认我是否删除子域并提交。

标签: ruby-on-rails ruby-on-rails-4 devise


【解决方案1】:

我设法通过在 Confirmation_instructions.html.erb 中使用以下内容来解决这个问题。这适用于我的设置,因为我使用的是 Apartment gem:

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

【讨论】:

  • 莫伊卡索米!你的方法也解决了我的问题。我想,我们都有与 Apartment gem 类似的设置,所以......我的问题:我在 application_controller 中将ActionMailer::Base.default_url_options[:host] = "#{subdomain}lvh.me:3000" 设置为 before_filter 函数;有没有办法在发送确认指令之前运行这个控制器/动作?
【解决方案2】:

您是否确认在您的 link_to 中,:subdomain =&gt; @resource.subdomain 部分实际上具有合法值?换句话说,你的“资源”(可能是用户模型)有一个subdomain 方法?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多