1

我已经阅读并尝试了许多 SO 帖子(即此处此处此处)中发布的解决方案,以及 Devise 关于如何在注册失败后更改路径的答案以及 Devise 的RegistrationsController 代码,但均无济于事。

而不是像大多数建议的那样在文件夹中执行自定义失败方法/lib/,看起来修复/覆盖它的最简单的地方是在RegistrationsController#create底部的方法中:

else
  clean_up_passwords resource
  respond_with resource
end

它(我假设)正确响应user(即将它们重定向到root_path/users),但这是棘手的部分:当我的用户注册时,我创建了一个嵌套模型,为此侵入 Devise 非常困难。我担心如果我弄乱了这个RegistrationsController#create方法,我会破坏我完美工作的嵌套模型。

有人还提到,Devise 解决方案对他们不起作用,但在更改路由问题后让它起作用。我怀疑我的情况是这样,但这是我的routes.rb文件以防万一:

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

  resources :users do
    resources :lockers
  end

  resources :lockers do
    resources :products
  end

  resources :products
  resources :lockups

  match '/user/:id', :to => 'users#show', :as => :user

  root :to => 'home#index'

我非常感谢任何人可以为我提供的任何帮助。我对 Rails 还是很陌生,但我总是乐于学习。

编辑:感谢 Passionate,我一直在试图弄清楚如何更改else注册控制器中最后一个块中的逻辑。这是我尝试过的(以及我的菜鸟逻辑):

  # Obviously cleared all the fields since it made no mention of resource
  # redirect_to root_path

  # Too many arguments since root_path takes 0
  # redirect_to root_path resource

  # Just bombed, something about a String somethingorother
  # render root_path

  # Heh, apparently call 'respond_with' and 'redirect_to' multiple times in one action
  # respond_with resource
  # redirect_to root_path
4

1 回答 1

0

首先你必须修复routes.rb文件。由于您使用的是自定义控制器,因此您还必须自定义devise_scope块。改变

get "signup", :to => "devise/registrations#new"

get "signup", :to => "registrations#new"

而且,如果您尝试覆盖该方法并想要设置root_path后设计轨道注册,您可以这样做

# app/controllers/registrations_controller.rb 
class RegistrationsController < Devise::RegistrationsController
   def create
     build_resource
     // The `build_resource` will build the users . 
    if resource.save
     if resource.active_for_authentication?
      set_flash_message :notice, :signed_up if is_navigational_format?
      sign_up(resource_name, resource)
      respond_with resource, :location => after_sign_up_path_for(resource)
     else
      set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
      expire_session_data_after_sign_in!
      respond_with resource, :location => after_inactive_sign_up_path_for(resource)
     end
    else

     clean_up_passwords resource
     ## replace your logic here
     redirect_to root_path
    end
   end
end

请注意,上面的代码适用于devise 2.2.4. 目前,由于 rails 4 和 strong_parameter 兼容性,github 上 master 分支上的代码有所更改。

于 2013-05-27T19:13:50.357 回答