在Rails中将student_id连接到Submitting_id [英] Connect student_id to submission_id in Rails
问题描述
我试图将我的student_id连接到submission_id,但是它不起作用.我有一个联接表,称为"subscriptionstudent".
I was trying to connect my student_id to the submission_id, but it's not working. I have a join table which is called submissionstudent.
Student Controller.rb:
Student Controller.rb:
class StudentsController < ApplicationController
before_action :set_student, only: [:show, :edit, :update, :destroy]
# GET /students
# GET /students.json
def index
@students = Student.all
end
# GET /students/1
# GET /students/1.json
def show
end
# GET /students/new
def new
@student = Student.new
end
# GET /students/1/edit
def edit
end
# POST /students
# POST /students.json
def create
@student = Student.new(student_params)
respond_to do |format|
if @student.save
format.html { redirect_to @student, notice: 'Student was successfully created.' }
format.json { render :show, status: :created, location: @student }
else
format.html { render :new }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /students/1
# PATCH/PUT /students/1.json
def update
respond_to do |format|
if @student.update(student_params)
format.html { redirect_to @student, notice: 'Student was successfully updated.' }
format.json { render :show, status: :ok, location: @student }
else
format.html { render :edit }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
# DELETE /students/1
# DELETE /students/1.json
def destroy
@student.destroy
respond_to do |format|
format.html { redirect_to students_url, notice: 'Student was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_student
@student = Student.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def student_params
params.require(:student).permit(:name)
end
end
Submission Controller.rb:
Submission Controller.rb:
class SubmissionsController < ApplicationController
before_action :set_submission, only: [:show, :edit, :update, :destroy]
before_action :set_form
# GET /submissions/new
def new
@submission = Submission.new
@all_students = Student.all
@submission_student = @submission.submissionstudent.build
end
# GET /submissions/1/edit
def edit
end
# POST /submissions
# POST /submissions.json
def create
@submission = Submission.new(submission_params)
@submission.form_id = @form.id
respond_to do |format|
if @submission.save
format.html { redirect_to @form, notice: 'Submission was successfully created.' }
format.json { render :show, status: :created, location: @submission }
else
format.html { render :new }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /submissions/1
# PATCH/PUT /submissions/1.json
def update
respond_to do |format|
if @submission.update(submission_params)
format.html { redirect_to @submission, notice: 'Submission was successfully updated.' }
format.json { render :show, status: :ok, location: @submission }
else
format.html { render :edit }
format.json { render json: @submission.errors, status: :unprocessable_entity }
end
end
end
# DELETE /submissions/1
# DELETE /submissions/1.json
def destroy
@submission.destroy
respond_to do |format|
format.html { redirect_to submissions_url, notice: 'Submission was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_submission
@submission = Submission.find(params[:id])
end
def set_form
@form = Form.find(params[:form_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def submission_params
params.require(:submission).permit(:conflict, :computer, :extra_time, :am_pm)
end
end
Student.rb模型:
Model Student.rb:
class Student < ActiveRecord::Base
has_many :submissionstudent
has_many :submissions, :through => :submissionstudent
end
Submission.rb模型:
Model Submission.rb:
class Submission < ActiveRecord::Base
belongs_to :form
has_many :submissionstudent
has_many :students, :through => :submissionstudent
end
加入表格模型Studentsubmission.rb:
Join table model Studentsubmission.rb:
class Submissionstudent < ActiveRecord::Base
belongs_to :submission
belongs_to :student
end
告诉我是否需要更多代码, 谢谢您的帮助
Tell me if you need any more code, Thanks for your help
推荐答案
您可能需要根据最佳做法更改association_name,table_name,model_name和file_name,我相信它会起作用
You might need to change the association_name, table_name, model_name and file_names as per the best practices and I am sure it will work
- 型号名称:CamelCase单数,例如
StudentSubmission
- 模型文件名:单数snake_case(_分隔),例如
student_submission
- 表名:复数
submissions
- 联接表名称:复数形式,例如
students_submissions
#表名按字母顺序排列的学生将在以后首先提交 - has_many关联:复数形式,例如
student_submissions
- 属于和has_one关联:单数,例如
submission
- Model name: Singular CamelCase e.g
StudentSubmission
- Model file name: singular snake_case (_ seperated) e.g.
student_submission
- Table name: plural
submissions
- Join table name: plural e.g.
students_submissions
# table name in alphabetical order student will come first submission later - has_many association: plural e.g.
student_submissions
- belongs_to and has_one association: Singular e.g.
submission
student.rb:
class Student < ActiveRecord::Base
has_many :student_submissions
has_many :submissions, :through => :student_submissions
end
submission.rb:
class Submission < ActiveRecord::Base
belongs_to :form
has_many :student_submissions
has_many :students, :through => :student_submissions
end
student_submission.rb:
class StudentSubmission < ActiveRecord::Base
# join_table_name: students_submissions
belongs_to :submission
belongs_to :student
end
您还需要在使用它们的任何地方更改关联
also you will need to change the associations where ever you are using them
@student_submission = @submission.student_submissions.build
这篇关于在Rails中将student_id连接到Submitting_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!