简体   繁体   English

使用 Devise Ruby On Rails 为新用户创建默认集合记录

[英]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在我的应用程序中,我有帖子和 Collections

_ Collection 1 _ 收藏 1

__ Post 1 __ 帖子 1

__ Post 2 __ 帖子 2

__ Post 3 __ 发布 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,所以我需要user_id,但在用户完成并注册之前我没有,

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 : )它现在正在使用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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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