我对rails非常陌生并且无法想出这个。
您好我想为每个用户提供多种配置文件。假设我想拥有教师资料和父资料。用户可以同时拥有两者。
我的用户模型包含教师和家长的所有字段,我希望能够通过
更新我的用户模型所以我有一个用户通过设计注册,然后他们可以创建一个教师和/或父配置文件(不互斥)。
我有以下这样的模型:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
class Teacher < User
end
class Parent < User
end
现在我不明白的问题是如何创建这些配置文件以及如何保存这些配置文件。
设计用户只需通过电子邮件,密码和&amp;密码确认字段。但是我已经将主题,学校,名称years_teaching_experience字段添加到用户表中以考虑我想要教师的额外字段。
在我的路线中,我添加了
resources :teachers
resources :parents
并创建了一个教师控制器:
class TeachersController < ApplicationController
before_action :set_teacher, only: [:show, :edit, :update, :destroy]
def new
@teacher = Teacher.new
end
def create
@teacher = Teacher.new(teacher_params)
if @teacher.save
redirect_to @teacher, notice: 'Teacher Profile was successfully created.'
else
render :new
end
end
def show
end
def edit
end
def update
end
private
def set_teacher
@teacher = Teacher.find(params[:id])
end
def teacher_params
params.require(:teacher).permit(:name, :subject, :school, :years_teaching_experience)
end
end
但是当我去/ teachers / new并提交此表格时:
<%= form_for @teacher do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :years_teaching_experience %><br />
<%= f.text_field :years_teaching_experience %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
我只是停留在基本呈现的同一页面上“#&new;&#39;因为某些原因我的信息未保存,因为创建操作显示:
def create
@teacher = Teacher.new(teacher_params)
if @teacher.save
redirect_to @teacher, notice: 'Teacher Profile was successfully created.'
else
render :new
end
end
我在服务器中看到一个params哈希值,因此传递了正确的参数但由于某种原因没有保存。
我是否误解了这些模型类之间的继承是如何工作的?如何将此教师配置文件保存到数据库中?
编辑: 这是我的参数:
Processing by TeachersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"h3mWY+jbqREmtlvShjFcKX2AoILFKklekI9XM9YI2/jHrvbQhp8KvDgFba0cr2MsI3qHUF84jkFZjN5Il6VEyQ==", "teacher"=>{"name"=>"John", "subject"=>"chemistry", "years_teaching_experience"=>"3"}, "commit"=>"Submit"}
(0.1ms) begin transaction
(0.1ms) rollback transaction
Rendered teachers/new.html.erb within layouts/application (1.7ms)