Redirect to root_path after failed Devise user registration

那年仲夏 提交于 2019-12-08 04:37:53

问题


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

回答1:


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 .



来源:https://stackoverflow.com/questions/16766849/redirect-to-root-path-after-failed-devise-user-registration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!