失败后重定向到root_path设计用户注册 [英] Redirect to root_path after failed Devise user registration

查看:114
本文介绍了失败后重定向到root_path设计用户注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过&尝试在许多SO帖子中发布的解决方案(即此处这里这里)以及Devise在如何在注册失败后更改路径,而Devise的 RegistrationsController code ,全部无效。



而不是像大多数建议一样在 / lib / 文件夹中执行自定义失败方法,似乎最简单的方法是修复/覆盖这个方法, code> RegistrationsController·C在方法的底部,它是:

  else 
clean_up_passwords资源
response_with资源
end

这是(我假设)使用用户(即将它们重定向到 root_path / users ),但这是一个棘手的部分:当我的用户注册时,我创建一个嵌套模型,并且这很难入侵Devise。我担心,如果我搞砸了 RegistrationsController#create 方法,那么我会打破我完美的嵌套模型。



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

  devise_for:users,:controllers => {:registrations => 注册} 
资源:用户

资源:用户做
资源:储物柜
结束

资源:储物柜
资源:产品
结束

资源:产品
资源:lockups

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

root:to => 'home#index'

我完全赞赏任何人都可以提供我的帮助。我还是很新的Rails,但我总是很高兴学习。



编辑:感谢热情,我一直在努力弄清楚如何更改最终 else 块在注册控制器中。这是我尝试过的(以及我的noob逻辑):

 #显然清除了所有的字段,因为它没有提到资源
#redirect_to root_path

#自root_path需要0以来参数太多
#redirect_to root_path资源

#只是轰炸了一些关于String的东西b $ b#render root_path

#Heh,显然在一个动作中多次调用respond_with和redirect_to
#respond_with resource
#redirect_to root_path


解决方案

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

  getsignup,:to => 设计/注册#新

  getsignup,:to => 注册#新

而且,如果您尝试覆盖该方法并想设置 root_path after devise rails注册后,可以像

 #app / controllers / registrations_controller.rb 
class RegistrationsController< Devise :: RegistrationsController
def create
build_resource
//build_resource将构建用户。
如果resource.save
如果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}如果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资源
##在这里替换你的逻辑
redirect_to root_path
end
end
end

请注意,上述代码与 devise 2.2 .4 。目前,由于rails 4和strong_parameter兼容性,在github上的master分支上的代码有所改变。


I've read & attempted the solutions posted in numerous SO posts (i.e. here, here, and here) as well as Devise's answer on how to change the path after a failed registration, and Devise's RegistrationsController code, all to no avail.

Instead of doing the custom failure method in the /lib/ folder like most suggest, It seems like the easiest place to fix/override this would be in the RegistrationsController#create method at the bottom where it's:

else
  clean_up_passwords resource
  respond_with resource
end

It's (I assume) correctly responding with the user (i.e. redirecting them to root_path/users), but here's the tricky part: I create a nested model when my user registers, and that was quite difficult hacking into Devise for that. I'm afraid that if I go messing with the RegistrationsController#create method, that I'll break my perfectly-working nested models.

Someone also mentioned that the Devise solution didn't work for them, but then got it to work after changing a routing problem. I doubt that's the case with me, but here's my routes.rb file just in case:

  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'

I completely appreciate any help anyone can provide me. I'm still pretty new to Rails, but I'm always happy to learn.

EDIT: Thanks to Passionate, I've been trying to figure out how to change the logic in the final else block in the registrations controller. Here's what I've tried (along with my noob logic):

  # 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

解决方案

First you have to fix routes.rb file . Since you're using custom controller , you also have to customize devise_scope block . Change

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

to

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

And, if you try to override the method and want to set the root_path after devise rails registration, you can do like

# 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

Please note that the above code works with devise 2.2.4 . Currently, the code which is on master branch on github is changed somewhat due to rails 4 and strong_parameter compatibility .

这篇关于失败后重定向到root_path设计用户注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆