Ruby on rails 联想中的设计与简单形式错误

Ruby on rails 联想中的设计与简单形式错误,ruby-on-rails,ruby,devise,simple-form,Ruby On Rails,Ruby,Devise,Simple Form,我在创建rails应用程序的注册时遇到问题,我使用的是Desive和Simple表单,我有两个模型(用户和部门),用户属于:部门和部门有:多个:用户,我在尝试注册时出错,说部门必须退出 设计/注册/new.html.erb <h2>Sign up</h2> <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>

我在创建rails应用程序的注册时遇到问题,我使用的是Desive和Simple表单,我有两个模型(用户和部门),用户属于:部门和部门有:多个:用户,我在尝试注册时出错,说部门必须退出

设计/注册/new.html.erb

    <h2>Sign up</h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :username, required: true, autofocus: true %>
    <%= f.input :email, required: true %>
    <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
    <%= f.input :password_confirmation, required: true %><br>
    <%= f.association :department %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>
迁移文件

class CreateDepartments < ActiveRecord::Migration[5.1]
  def change
    create_table :departments do |t|
      t.string :name
      t.text :description

      t.timestamps
    end
  end
end
# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""
      t.string :username,           null: false, default: ""
      t.references :department, foreign_key: true

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

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    add_index :users, :username,             unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end
class CreateCourses < ActiveRecord::Migration[5.1]
  def change
    create_table :courses do |t|
      t.string :name
      t.text :description
      t.references :department, foreign_key: true
      t.string :instructor_name
      t.integer :credit_hours

      t.timestamps
    end
    create_table :enrollments do |t|
      t.references :user, foreign_key: true
      t.references :courses, foreign_key: true

      t.timestamps
    end
  end
end
class CreateDepartments
另外,我刚开始学习rails,谢谢你的帮助


Rails自5.1左右以来,有一个必需的属性,用于对新生成的应用程序进行验证

您可以通过以下方式禁用该功能:

class User < ApplicationRecord
  belongs_to :department, optional: true
end
class用户

通过这种方式,您可以先创建部门为空的用户。

Deviate不知道您的非标准
部门id
字段的任何信息,并将其作为未经允许的参数进行筛选

创建您自己的注册控制器(扩展Desive),然后自定义以下方法:

def sign_up_params
  params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :department_id)
end

这只是一个样本。用你的真实字段名来感受它

部门表是否至少有一条记录?是的,它有两个条目。这会生成一个表条目,但没有部门id,而且我注意到没有保存用户名谢谢你,我照你说的做了,它起了作用,制作了一个控制器来扩展Desive,并将注册参数添加为private,并添加了def new super和def edit super。新条目的用户名和部门id都有效。欢迎您,还有。。。。您不需要明确提及new/edit/等。您只需编写一个非常简单的注册参数即可。默认情况下,其他所有内容都将映射到父级
class CreateDepartments < ActiveRecord::Migration[5.1]
  def change
    create_table :departments do |t|
      t.string :name
      t.text :description

      t.timestamps
    end
  end
end
# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""
      t.string :username,           null: false, default: ""
      t.references :department, foreign_key: true

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

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    add_index :users, :username,             unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end
class CreateCourses < ActiveRecord::Migration[5.1]
  def change
    create_table :courses do |t|
      t.string :name
      t.text :description
      t.references :department, foreign_key: true
      t.string :instructor_name
      t.integer :credit_hours

      t.timestamps
    end
    create_table :enrollments do |t|
      t.references :user, foreign_key: true
      t.references :courses, foreign_key: true

      t.timestamps
    end
  end
end
class User < ApplicationRecord
  belongs_to :department, optional: true
end
def sign_up_params
  params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :department_id)
end