Ruby on rails 创建我的第一个关联对象时遇到问题:学校的教师
要在web浏览器中创建学校内的教师。不确定具体需要什么代码来执行此操作。使用脚手架创建学校,控制器和模型创建教师。我的目标是在创建一所学校之后,能够增加教师 学校模式Ruby on rails 创建我的第一个关联对象时遇到问题:学校的教师,ruby-on-rails,Ruby On Rails,要在web浏览器中创建学校内的教师。不确定具体需要什么代码来执行此操作。使用脚手架创建学校,控制器和模型创建教师。我的目标是在创建一所学校之后,能够增加教师 学校模式 class School < ActiveRecord::Base has_many :teachers end 班级学校
class School < ActiveRecord::Base
has_many :teachers
end
班级学校
教师模式
class Teacher < ActiveRecord::Base
belongs_to :school
end
班主任
校董
class SchoolsController < ApplicationController
before_action :set_school, only: [:show, :edit, :update, :destroy]
# GET /schools
# GET /schools.json
def index
@schools = School.all
end
# GET /schools/1
# GET /schools/1.json
def show
end
# GET /schools/new
def new
@school = School.new
end
# GET /schools/1/edit
def edit
end
# POST /schools
# POST /schools.json
def create
@school = School.new(school_params)
respond_to do |format|
if @school.save
format.html { redirect_to @school, notice: 'School was successfully created.' }
format.json { render :show, status: :created, location: @school }
else
format.html { render :new }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schools/1
# PATCH/PUT /schools/1.json
def update
respond_to do |format|
if @school.update(school_params)
format.html { redirect_to @school, notice: 'School was successfully updated.' }
format.json { render :show, status: :ok, location: @school }
else
format.html { render :edit }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schools/1
# DELETE /schools/1.json
def destroy
@school.destroy
respond_to do |format|
format.html { redirect_to schools_url, notice: 'School was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def school_params
params.require(:school).permit(:name)
end
end
class TeachersController < ApplicationController
end
class-SchoolsController
教师总监
class SchoolsController < ApplicationController
before_action :set_school, only: [:show, :edit, :update, :destroy]
# GET /schools
# GET /schools.json
def index
@schools = School.all
end
# GET /schools/1
# GET /schools/1.json
def show
end
# GET /schools/new
def new
@school = School.new
end
# GET /schools/1/edit
def edit
end
# POST /schools
# POST /schools.json
def create
@school = School.new(school_params)
respond_to do |format|
if @school.save
format.html { redirect_to @school, notice: 'School was successfully created.' }
format.json { render :show, status: :created, location: @school }
else
format.html { render :new }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schools/1
# PATCH/PUT /schools/1.json
def update
respond_to do |format|
if @school.update(school_params)
format.html { redirect_to @school, notice: 'School was successfully updated.' }
format.json { render :show, status: :ok, location: @school }
else
format.html { render :edit }
format.json { render json: @school.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schools/1
# DELETE /schools/1.json
def destroy
@school.destroy
respond_to do |format|
format.html { redirect_to schools_url, notice: 'School was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@school = School.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def school_params
params.require(:school).permit(:name)
end
end
class TeachersController < ApplicationController
end
类教师控制器
学校/风景/表演
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @school.name %>
</p>
<%= link_to 'Edit', edit_school_path(@school) %> |
<%= link_to 'Back', schools_path %>
名称:
|
您可以在学校或教师控制器中执行此操作,您可能希望它处于创建状态,当然是更新状态。假设学校控制器和params包含教师名称为:teacher_name
def update
@school.teachers = Teacher.new(school_params[:teacher_name])
respond_to do |format|
... snip for brevity ...
end
end
我假设您的数据库表已经使用[migrations]进行了设置。从教师端:
在教师新页面(表单)中,您需要有一个选择框,其中包含教师可以归属的学校,这些学校的ID值(来自数据库)。教师模型将学校id作为其属性之一,一旦您进入rails控制台并键入类似于学校的内容。首先,教师您将获得该学校所有教师的列表。inye:我尝试了几种不同的方法,但在如何在学校/节目中创建教师的问题上,我遇到了麻烦。我希望学校用户能够创建教师,一旦登录到帐户。希望有道理。谢谢马特。我要试试这个。正如我在上面的回复中提到的,我希望学校用户能够在他们的学校中创建教师。这意味着现在,我的目标是让“教师创造形式”在学校表演中可见。上面的代码很有帮助,我会努力完成这项工作。谢谢你的回复。我能够按照你描述的方式成功地完成它。我更倾向于学校用户,他们可以选择创建教师作为学校的一部分,而不是相反。可以这样想:您是一所学校的管理员,登录后,希望将教师添加到您的学校。这就是我的目标。在我原来的帖子里,我可以说得更清楚。希望这是有道理的。