Devise controllers rails

前端 未结 2 1892
醉酒成梦
醉酒成梦 2021-02-06 16:39

I\'m using Rails 3, on ruby 1.8.7. And using for auth. devise (1.1.3). But it is a quite large community site i\'m building, so i have a table for profiles and a table for users

相关标签:
2条回答
  • 2021-02-06 17:18

    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
    
    0 讨论(0)
  • 2021-02-06 17:28

    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" }
    
    0 讨论(0)
提交回复
热议问题