Devise controllers rails

若如初见. 提交于 2019-12-03 03:47:26

You can subclass the Devise RegistrationsController and add your own logic in the create() method, and call the parent class methods for everything else.

class MyRegistrationsController < Devise::RegistrationsController
  prepend_view_path "app/views/devise"

  def create
    super
    # Generate your profile here
    # ...
  end

  def update
    super
  end
end

If you want to customise the Devise views that are packaged inside the Gem then you can run the following command to generate the view files for your app:

rails generate devise:views

You will also need to tell the router to use your new controller; something like:

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

There's not really any need to involve the controller in this; models can (and should) do all of the heavy lifting here.

I'm assuming that you have a relationship between User and Profile models, in which case, you should just be able to do something like this:

class User < ActiveRecord::Base
  has_one :profile # could be a belongs_to, but has_one makes more sense

  after_create :create_user_profile

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