设计多个模型,全名= nil

时间:2018-10-24 10:28:19

标签: ruby-on-rails devise

我在自己的应用程序上使用了devise gem。 我创建了2个模型(具有两个不同的注册表格)。

但是当我通过db将全名和名称添加到表单中时。注册或更改个人资料时,它发送的信息为零。其他一切都工作正常。

user.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

editor.rb

class Editor < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :softwares
end

controller / editors / registrations_controller.rb

class Editors::RegistrationsController < Devise::RegistrationsController
end

controller / users / registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
end

db / migrate / devise_create_users.rb

class DeviseCreateUsers < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|

    t.string :email,              null: false, default: ""
    t.string :encrypted_password, null: false, default: ""
    t.string :name
    t.string :fullname 

   end
  end
end

与编辑相同

感谢您的帮助...

1 个答案:

答案 0 :(得分:1)

我希望它能解决您的问题...

controller / editors / registrations_controller.rb

class Editors::RegistrationsController < Devise::RegistrationsController

  private

  def sign_up_params
    params.require(:editor).permit(:name, :fullname, :email, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:editor).permit(:name, :fullname, :email, :password, :password_confirmation, :current_password)
  end
end

controller / users / registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController

  private

  def sign_up_params
    params.require(:user).permit(:name, :fullname, :email, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:user).permit(:name, :fullname, :email, :password, :password_confirmation, :current_password)
  end
end

并将此行添加到您的config / routes.rb文件中

devise_for :users, :controllers => { registrations: 'registrations' }
devise_for :editors, :controllers => { registrations: 'registrations' }

要了解更多click here