use*_*den 1 ruby-on-rails devise ruby-on-rails-4
我正在使用 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
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用,因为用户的子域不在链接上,它应该是例如
http://mysubdomain.lvh.me:3000/users/confirmation?confirmation_token=xsTNUw5oQYTPf_CBwZXD
Run Code Online (Sandbox Code Playgroud)
如果我在浏览器上手动添加子域链接,它会确认该帐户。
如何更改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>
Run Code Online (Sandbox Code Playgroud)
我尝试过例如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...>"
Run Code Online (Sandbox Code Playgroud)
这是我的 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
Run Code Online (Sandbox Code Playgroud)
这是我的路线:
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
Run Code Online (Sandbox Code Playgroud)
在development.rb中我设置了:
config.action_mailer.default_url_options = { host: "lvh.me:3000" }
Run Code Online (Sandbox Code Playgroud)
在应用程序控制器中我有:
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
Run Code Online (Sandbox Code Playgroud)
这是控制台上的错误:
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'
Run Code Online (Sandbox Code Playgroud)
我设法通过在confirmation_instructions.html.erb中使用以下内容来解决这个问题。这适用于我的设置,因为我使用的是 Apartment gem:
<%= link_to 'Confirm my account', user_confirmation_url(confirmation_token: @token, subdomain: Apartment::Tenant.current) %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
959 次 |
| 最近记录: |