简体   繁体   English

Devise:使用带Devise的用户控制器创建“个人资料”

[英]Devise: creating a “profile” using users controller with Devise

I'm working on a Rails 6 application where I was separating the User and Profile models. 我正在使用Rails 6应用程序,在其中我将用户模型和个人档案模型分离了。 This caused some issues as shown in this post Rails 6: Only one profile per user should be created I'm using Devise for authentication and decided to add a users controller. 这引起了一些问题,如这篇文章中的Rails 6所示:每个用户只能创建一个配置文件。我正在使用Devise进行身份验证,并决定添加一个用户控制器。 I want to be able to User the user model as a profile and that Devise takes care of the sign_up, sign_in and the creation of the user. 我希望能够将用户模型作为配置文件使用,并且Devise负责sign_up,sign_in和用户的创建。

I created the user's controller to show , edit , update and destroy and would use this as the "profile".However, I'm confused about the following. 我创建了用于showeditupdatedestroy用户的控制器,并将其用作“配置文件”。但是,我对以下内容感到困惑。 How to make Devise work with the users_controller that inherits from ApplicationRecord wouldn't this 2 conflict? 如何使设计与工作users_controller从继承ApplicationRecord不会这2冲突?

class DeviseCreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Profile information
      t.string :full_name
      t.string :city
      t.string :bio


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

I added the other fields into the DeviseCreateUsers migration. 我将其他字段添加到DeviseCreateUsers迁移中。 Should I create a separate create_users_migration ? 我应该创建一个单独的create_users_migration吗? I would like for a user to sign up with email and password and not necessarily need the fields :full_name , :city , :bio to create the instance of User. 我希望用户使用电子邮件和密码进行注册,而不一定需要使用:full_name:city:bio字段来创建User实例。

routes.rb routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers: {
       sessions: 'users/sessions'
     }
  resources :users, only: [:show, :edit, :update, :destroy]

  resources :posts do
    resource :comments, only: %i[show new create edit update]
  end
end

When I followed the Devise documentation: https://github.com/plataformatec/devise#configuring-controllers I'm a bit confused about the followin. 当我遵循Devise文档时: https : //github.com/plataformatec/devise#configuring-controllers对于跟随者我有些困惑。 I run the command 我运行命令

$ rails generate devise:controllers users

在此处输入图片说明

Does the documentation is unclear as to whether one should add the views of log_in , sign_up those forms into the users folder or only new.html.erb form devise/sessions which is the only template that is generated. 是否文档不清楚一个是否应该添加的意见log_insign_up的形式进入users文件夹或仅new.html.erb形式devise/sessions这是产生唯一的模板。

I can't answer your whole question, but I can tell you that you don't need to have all fields filled in to create an instance of user. 我无法回答您的整个问题,但是我可以告诉您,您无需填写所有字段即可创建用户实例。 If you have different fields for sign up and edit form that would work just fine. 如果您有其他字段用于注册和编辑表格,那将很好。

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

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