【问题标题】:Devise: creating a "profile" using users controller with Devise设计:使用带有设计的用户控制器创建“配置文件”
【发布时间】:2019-06-16 20:59:18
【问题描述】:

我正在开发一个 Rails 6 应用程序,在该应用程序中我将 User 和 Profile 模型分开。这导致了一些问题,如这篇文章中所示Rails 6: Only one profile per user should be created 我正在使用 Devise 进行身份验证,并决定添加一个用户控制器。我希望能够将用户模型作为配置文件使用,并且 Devise 负责注册、登录和用户的创建。

我为showeditupdatedestroy 创建了用户控制器,并将其用作“配置文件”。但是,我对以下内容感到困惑。如何使设计与继承自ApplicationRecordusers_controller 一起工作不会这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

我将其他字段添加到 DeviseCreateUsers 迁移中。我应该创建一个单独的create_users_migration 吗?我希望用户使用电子邮件和密码注册,并且不一定需要字段:full_name:city:bio 来创建用户实例。

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

当我遵循设计文档时:https://github.com/plataformatec/devise#configuring-controllers 我对以下内容有点困惑。我运行命令

$ rails generate devise:controllers users

文档是否不清楚是否应该将log_insign_up 这些表单的视图添加到users 文件夹中,或者仅将new.html.erb 表单添加到devise/sessions 中,这是生成的唯一模板。

【问题讨论】:

    标签: ruby-on-rails devise ruby-on-rails-6


    【解决方案1】:

    我无法回答您的全部问题,但我可以告诉您,您不需要填写所有字段来创建用户实例。如果您有不同的字段用于注册和编辑表单,那就可以了。

    【讨论】:

    • 这不是一个合适的答案。请记住,社区用户很可能是技术人员,并且希望得到这样的答案。这更适合发表评论,因为它不回答问题并且是您提供的附加信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    相关资源
    最近更新 更多