用户确认帐户后重写重定向路径 - 设计

时间:2013-05-07 09:00:22

标签: ruby-on-rails devise

我想要发生的是,一旦用户点击确认链接 - 并且他们的帐户已成功确认 - 他们应该被重定向到某个指定的路径,如果他们分配了特定的角色(已成功分配, after_create模型上的User回调。

我创建了一个RegistrationsController

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    if resource.has_role? :seller
      new_item_path
    else
      root_path
    end
  end
end

但是,这总是重定向到root_path ....即使我已经验证过,用户确实有这个角色。

修改1

似乎确认请求永远不会发送到RegistrationsController

Started GET "/users/confirmation?confirmation_token=KRwZ7MChtxxq4sxxkDLq" for 127.0.0.1 at 2013-05-07 03:52:56 -0500
Processing by Devise::ConfirmationsController#show as HTML
  Parameters: {"confirmation_token"=>"KRwZ7MChtxxq4sxxkDLq"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'KRwZ7MChtxxq4sxxkDLq' LIMIT 1
   (0.1ms)  BEGIN
   (0.5ms)  UPDATE "users" SET "confirmation_token" = NULL, "confirmed_at" = '2013-05-07 08:52:56.846852', "updated_at" = '2013-05-07 08:52:56.847730' WHERE "users"."id" = 9
   (0.8ms)  COMMIT
   (0.1ms)  BEGIN
   (0.4ms)  UPDATE "users" SET "last_sign_in_at" = '2013-05-07 08:52:56.852250', "current_sign_in_at" = '2013-05-07 08:52:56.852250', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_ip" = '127.0.0.1', "sign_in_count" = 1, "updated_at" = '2013-05-07 08:52:56.853180' WHERE "users"."id" = 9
   (0.7ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 31ms (ActiveRecord: 3.2

修改2

这就是我routes.rb的样子

  devise_for :users, :path_names => { :sign_up => "register", 
                                      :sign_in => "login", 
                                      :sign_out => "logout",
                                      :settings => "settings" },
                      :controllers => { :registrations => "registrations" }

  devise_scope :user do
    get "login", :to => "devise/sessions#new"
    get "register", :to => "registrations#new"
        get "settings", :to => "devise/registrations#edit"
    get "logout",   :to => "devise/sessions#destroy"    
  end

3 个答案:

答案 0 :(得分:16)

我最终想通了。

我必须创建一个覆盖Devise的确认控制器的ConfirmationsController.rb类。

class ConfirmationsController < Devise::ConfirmationsController
  protected
    def after_confirmation_path_for(resource_name, resource)
      if resource.has_role? :seller
        new_item_path
      else
        root_path
      end
    end
end

然后在我的routes.rb

  devise_for :users, :path_names => { :sign_up => "register", 
                                      :sign_in => "login", 
                                      :sign_out => "logout",
                      :settings => "settings" },
                      :controllers => {:confirmations => "confirmations"}

  devise_scope :user do
    get "login", :to => "devise/sessions#new"
    get "register", :to => "devise/registrations#new"
    get "settings", :to => "devise/registrations#edit"
    get "logout",   :to => "devise/sessions#destroy"    
  end

这似乎对我来说很好。

答案 1 :(得分:0)

您是否在模型user.rb中编写了角色方法。如果不是,你应该写角色方法

OR

您可以在顶部的用户类的user.rb中编写rolify

答案 2 :(得分:0)

你有没有写下来告诉设计使用被覆盖的控制器而不是默认的

在config / routes.rb

devise_for :users, :controllers => {:registrations => "registrations"}