我正在开发一个Rails 5项目,我正在使用Device使用多态关系创建多个模型,如下所示:
class DeviseCreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :meta_type
t.integer :meta_id
## 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
## 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, :meta_id
add_index :users, :meta_type
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
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
belongs_to :meta, polymorphic: true
end
我有一个Caregiver课程(我稍后会添加护理人员特定的字段)
class CreateCaregivers < ActiveRecord::Migration[5.0]
def change
create_table :caregivers do |t|
t.timestamps
end
end
end
caregiver.rb
class Caregiver < ApplicationRecord
has_one :user, as: :meta, dependent: :destroy
accepts_nested_attributes_for :user
end
我为照顾者类搭建了控制器。这是文件:
class CaregiversController < ApplicationController
before_action :set_caregiver, only: [:show, :edit, :update, :destroy]
# GET /caregivers
# GET /caregivers.json
def index
@caregivers = Caregiver.all
end
# GET /caregivers/1
# GET /caregivers/1.json
def show
end
# GET /caregivers/new
def new
@caregiver = Caregiver.new
@caregiver.build_user
end
# GET /caregivers/1/edit
def edit
end
# POST /caregivers
# POST /caregivers.json
def create
@caregiver = Caregiver.new(caregiver_params)
respond_to do |format|
if @caregiver.save
format.html { redirect_to @caregiver, notice: 'Caregiver was successfully created.' }
format.json { render :show, status: :created, location: @caregiver }
else
format.html { render :new }
format.json { render json: @caregiver.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /caregivers/1
# PATCH/PUT /caregivers/1.json
def update
respond_to do |format|
if @caregiver.update(caregiver_params)
format.html { redirect_to @caregiver, notice: 'Caregiver was successfully updated.' }
format.json { render :show, status: :ok, location: @caregiver }
else
format.html { render :edit }
format.json { render json: @caregiver.errors, status: :unprocessable_entity }
end
end
end
# DELETE /caregivers/1
# DELETE /caregivers/1.json
def destroy
@caregiver.destroy
respond_to do |format|
format.html { redirect_to caregivers_url, notice: 'Caregiver was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_caregiver
@caregiver = Caregiver.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def caregiver_params
params.require(:caregiver).permit(user_attributes: [ :id, :email, :password, :password_confirmation ])
end
end
这是创建表单
<%= form_for(@caregiver) do |f| %>
<% if @caregiver.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@caregiver.errors.count, "error") %> prohibited this caregiver from being saved:</h2>
<ul>
<% caregiver.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.fields_for :user do |u| %>
<div class="col-lg-6">
<%= u.label :email %><br>
<%= u.text_field :email %>
</div>
<div class="col-lg-6">
<%= u.label :password %><br>
<%= u.text_field :password %>
</div>
<div class="col-lg-6">
<%= u.label :password_confirmation %><br>
<%= u.text_field :password_confirmation %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我正在关注this示例。
Github上的示例运行正常。但是,当我尝试创建新的看护者记录时,我收到以下错误:
用户元必须存在
我无法找到错误。
答案 0 :(得分:1)
Rails 5默认需要belongs_to
关联。要禁用它,您需要将optional: true
添加到belongs_to
belongs_to :meta, polymorphic: true, optional: true