Ошибка Devise и Simple_form в ассоциации - PullRequest
0 голосов
/ 02 мая 2018

У меня проблемы с созданием. Подпишитесь на мое приложение rails, я использую Devise и Simple_form, у меня есть 2 модели (Пользователь и отдел), пользователи. попробуйте зарегистрироваться, сказав, что отдел должен выйти.

изобрести / регистрация / 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" %>

user.rb :

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates_uniqueness_of :email, :username
  belongs_to :department
  has_and_belongs_to_many :courses
end

Department.rb :

class Department < ApplicationRecord
  has_many :users
  has_many :courses
end

Я заполнил таблицу отделов с использованием seed.rb и проверил через консоль mysql.

schema.rb :

ActiveRecord::Schema.define(version: 20180502071349) do

  create_table "courses", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
    t.string "name"
    t.text "description"
    t.bigint "department_id"
    t.string "instructor_name"
    t.integer "credit_hours"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["department_id"], name: "index_courses_on_department_id"
  end

  create_table "departments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
    t.string "name"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "enrollments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
    t.bigint "user_id"
    t.bigint "courses_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["courses_id"], name: "index_enrollments_on_courses_id"
    t.index ["user_id"], name: "index_enrollments_on_user_id"
  end

  create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "username", default: "", null: false
    t.bigint "department_id"
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    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"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["department_id"], name: "index_users_on_department_id"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["username"], name: "index_users_on_username", unique: true
  end

  add_foreign_key "courses", "departments"
  add_foreign_key "enrollments", "courses", column: "courses_id"
  add_foreign_key "enrollments", "users"
  add_foreign_key "users", "departments"
end

миграционные файлы :

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

П.С., я только начинаю с рельсов и спасибо за вашу помощь.

Снимок экрана с ошибкой:

Ответы [ 2 ]

0 голосов
/ 02 мая 2018

Devise ничего не знает о вашем нестандартном поле department_id и фильтрует его как недопустимый параметр.

Создайте свой собственный контроллер регистрации (который расширяет Devise) и затем настройте эти методы:

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

Это всего лишь образец. Почувствуйте это с вашими реальными именами полей

0 голосов
/ 02 мая 2018

Rails начиная с версии 5.1 или около того имеет обязательную проверку принадлежность к вновь создаваемым приложениям.

Вы можете отключить это:

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

Таким образом, вы можете сначала создать пользователей с пустым отделом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...