祝你一切安好。
在我的应用程序中,我有Posts和Collections
_ Collection 1
__ Post 1
__ Post 2
__ Post 3
我需要为新用户创建一个默认集合。我需要user_id,但在用户完成注册之前我还没有,
当用户首次登录时,我应该检查登录计数并创建集合吗?还是有更好的办法?谢谢你
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。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
路线devise_for :users, controllers: {
confirmations: 'users/confirmations',
passwords: 'users/passwords',
registrations: 'users/registrations',
sessions: 'users/sessions'
}, layout: 'devise'