简体   繁体   中英

Create a default Collection record for new user with Devise Ruby On Rails

I hope you are well.

In my app, I have Posts and Collections

_ Collection 1

__ Post 1

__ Post 2

__ Post 3

I need to create a default collection for new user. So I need user_id, but I don't have it before user completes and sign up,

Should I check sign in count and create collection when user first signs in? or is there a better way? Thank you

   def after_sign_in_path_for(resource)
        if resource.sign_in_count == 1
           Collection.create(title: 'default collection', user_id: current_user.id)
        end
    end

OK. it is working now with after_action : )

class Users::RegistrationsController < Devise::RegistrationsController
  after_action :create_first_coll, only: [:create]

  def create_first_coll
    Collection.create(title: "First", user_id: @user.id)
  end

route

  devise_for :users, controllers: {
    confirmations: 'users/confirmations',
    passwords: 'users/passwords',
    registrations: 'users/registrations',
    sessions: 'users/sessions'
  }, layout: 'devise'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM