2010-10-21 58 views
3

如何在邮件视图中自定义由Devise生成的默认行?在Devise中自定义confirmation_url

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

我在我的控制器中写了一个名为user_confirm的方法。我也为它定义了一条路线。我可以通过令牌获得链接到该方法的URL作为参数吗?

回答

7

我用这个路由:

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

这ERB:

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

,得到了这个漂亮的链接:

http://localhost:3000/confirm/RjOnrd5yNREEDwsEfiFa 
+0

我应该做哪些改变,如果我希望用户被重定向到生产HTTPS? – 2014-02-22 07:14:05

+0

我现在发送的链接是http,并且用户也获取他的令牌,但由于该应用托管在https上,因此它无法识别令牌: – 2014-02-22 07:15:00

+0

设计3更新:设计现在使用一个较短的标记,然后在比较数据库之前进行散列。您的邮件链接应该如下所示:'link_to'确认我的帐户',confirmation_url(@resource,confirmation_token:@token)' – steakchaser 2014-12-30 19:07:27

0

明白了。 可以说我定义了我这样的命名路线。

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

我不得不这样做是

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

它像(在routes.rb中):

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

和意见,你可以使用类似:

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

为Rails 3

+0

谢谢!有效! – 2012-10-15 13:04:22

+0

这似乎是比接受的更好的答案,除非我误解。这个与Rails 3.2一起工作。 注意:''devise/confirmations#show'明确表示使用默认的设计确认控制器。如果您有自定义确认控制器,请改用'confirmations#show',否则会出现意外的行为。 – John 2013-08-20 01:34:20

2
  • rails 4.0.5
  • devise 3.2.4

网址:

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

应用程序/视图/设计/邮件/ confirmation_instructions.html.erb:

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

配置/ routes.rb中:

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

应用/视图/设计/邮寄者/ confirmation_instructions.html。ERB:

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

网址:

http://example.com/confirm/Kezap1iutgvXyQAhyu64 
0

定制色器件URL将用户表的不UDPATE 'confirmed_at' 栏,你可以做的是点击确认链接后,将用户重定向:

步骤1 覆盖您的confirmations_controller中的after_confirmation_path_for:

创建app/controllers目录新confirmations_controller.rb:

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

STEP 2在配置/ routes.rb中,加入这一行,这样设计会使用自定义ConfirmationsController。这假定Devise在用户表上运行(您可以编辑以匹配您的用户)。

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

STEP 3重新启动Web服务器

相关问题