为什么设计会生成这种格式的确认URL?

时间:2018-12-05 13:40:48

标签: ruby-on-rails-4 devise devise-confirmable devise-token-auth

设计继续生成confirmation URL的这种格式

http://something.com/users/confirmation/divyanshu-rawat?confirmation_token=CV3zV1wAWsb3RokHHEKN

我不知道为什么它不生成这样的东西。

http://something.com/users/confirmation?confirmation_token=CV3zV1wAWsb3RokHHEKN

这就是我的confirmation_instructions.html.haml的样子。

%p Welcome #{@resource.first_name}!
%p You can confirm your account email through the link below:
%p= link_to 'Confirm my account', user_confirmation_url(@resource, :confirmation_token => @resource.confirmation_token)

1 个答案:

答案 0 :(得分:1)

Devise gem中,创建确认路线的方法如下,

#  # Confirmation routes for Confirmable, if User model has :confirmable configured
#  new_user_confirmation GET    /users/confirmation/new(.:format) {controller:"devise/confirmations", action:"new"}
#      user_confirmation GET    /users/confirmation(.:format)     {controller:"devise/confirmations", action:"show"}
#                        POST   /users/confirmation(.:format)     {controller:"devise/confirmations", action:"create"}

因此,如果您想创建类似的网址,

http://something.com/users/confirmation?confirmation_token=CV3zV1wAWsb3RokHHEKN

使用

user_confirmation_url(confirmation_token: @resource.confirmation_token)`

代替

user_confirmation_url(@resource, confirmation_token: @resource.confirmation_token)`

还要检查 routes.rb

如果您要在确认URL中传递@resource user_name name db属性(如您在URL中传递'divyanshu-rawat'所要求的),您可以创建自己的自定义路由,该路由将指向以下相同的控制器和操作,

  # config/routes.rb
  devise_for :users

  as :user do
    get  '/users/confirmation/:name' => "devise/confirmations#show", as: 'user_confirm'
  end 

如果您的情况是@ resource.user_name ='divyanshu-rawat',请如下更新confirmation_instructions.html.haml

%p Welcome #{@resource.first_name}!
%p You can confirm your account email through the link below:
%p= link_to 'Confirm my account', user_confirm_url(name: @resource.user_name, confirmation_token: @resource.confirmation_token)

哪个会产生类似网址,

http://something.com/users/confirmation/divyanshu-rawat?confirmation_token=CV3zV1wAWsb3RokHHEKN