Extending Devise Registration Controller

ぐ巨炮叔叔 提交于 2019-12-19 02:47:11

问题


I have an Accounts model set up with Devise. Devise uses a few attributes (such as email, password etc.) but I have a few other attributes that I made. On sign up, I'd like to set them up in a way.

How can I extend the registration controller? I understand that I need to actually create a new controller like this:

class AccountsController < Devise::RegistrationController
  def create
    super
  end
end

Can I just add my code right after super? I think it would be too late as the resource would have already been saved. What's the best way to do this?

If I were to write create from scratch, how would I know that I didn't miss anything that Devise does?

Thanks,


回答1:


If you want to rewrite the controller from scratch for full control, start with the registration_controller.rb Source Code and make your changes as necessary.

Telling devise to use your custom controller is as simple as changing the route:

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



回答2:


According to the Devise documentation, yes, just like Josh's answer, you would change the controller. Although, you don't have to start completely from scratch. Take a look at the documentation.

You can generate the controller so you are able to add customizations:

Example: rails generate devise:controllers [scope]

So, you could run the following for your Users scope:

rails generate devise:controllers users

This gives you the controllers in a folder located here: app/controllers/users

Then, tell your routes file to use that controller. Update your devise route to look like this:

devise_for :users, controllers: { sessions: "users/sessions" }

And finally, copy over all the views. If you haven't genereated the views yet, you'll need to do so. The controller has changed, so your views will need to as well.




回答3:


If you want to fields for user supplied information there's no need to extend controller.
If you want to add those automatically, there's no reason not to do it in model! (unless it depends on session or request)

In 1st case, see https://github.com/plataformatec/devise#configuring-views

You should change DEvise controller ONLY if you intend to change signup flow.



来源:https://stackoverflow.com/questions/17112392/extending-devise-registration-controller

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