开发者

Customizing the confirmation_url in Devise

开发者 https://www.devze.com 2023-01-20 23:12 出处:网络
How do you customize this default line generated by Devise in the mailer view? <开发者_StackOverflow社区p><%= link_to \'Confirm my account\', confirmation_url(@resource, :confirmation_token

How do you customize this default line generated by Devise in the mailer view?

<开发者_StackOverflow社区p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %></p>

I've written a method in my controller called user_confirm. And I have also defined a route for it. Can I get the URL to link to that method with token as the params?


I used this routing:

map.user_confirm 'confirm/:confirmation_token',
    :controller => 'confirmations', :action => 'show'

And this ERB:

<%= link_to 'Confirm my account',
    user_confirm_url(:confirmation_token => @resource.confirmation_token) %>

And got this nice link:

http://localhost:3000/confirm/RjOnrd5yNREEDwsEfiFa


Its something like (in routes.rb):

devise_scope :user do
  match '/confirm/:confirmation_token', :to => "devise/confirmations#show", :as => "user_confirm", :only_path => false
end

and in views you can use something like:

<%= link_to 'Confirm my account', user_confirm_url(@resource.confirmation_token) %>

for Rails 3.


  • rails 4.0.5
  • devise 3.2.4

before

url:

http://example.com/users/confirmation?confirmation_token=jevYKv1z9Pr1LsAUB2NX

app/views/devise/mailer/confirmation_instructions.html.erb:

<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>

after

config/routes.rb:

devise_scope :user do
    get 'confirm/:confirmation_token', to: 'devise/confirmations#show'
end

app/views/devise/mailer/confirmation_instructions.html.erb:

<p><%= link_to 'Confirm my acount', confirm_url(@token) %></p>

url:

http://example.com/confirm/Kezap1iutgvXyQAhyu64


Got it. lets say i define i named route like this.

map.user_confirm '/user_confirm',  :controller => 'users', :action => 'confirm'

all i had to do is

<p><%= link_to 'Confirm my account', user_confirm_url(confirmation_token => @resource.confirmation_token) %></p>


Customizing the devise URL will not udpate 'confirmed_at' column of the user table, what you can do is redirect the user after clicking the confirmation link:

STEP 1 override the after_confirmation_path_for in your confirmations_controller:

Create a new confirmations_controller.rb in app/controllers directory:

class ConfirmationsController < Devise::ConfirmationsController
  private
  def after_confirmation_path_for(resource_name, resource)
  your_new_after_confirmation_path
 end
end

STEP 2 In config/routes.rb, add this line so that Devise will use your custom ConfirmationsController. This assumes Devise operates on users table (you may edit to match yours).

devise_for :users, controllers: { confirmations: 'confirmations' }

STEP 3 Restart the web server

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号