首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Rails中将student_id连接到submission_id

在Rails中将student_id连接到submission_id
EN

Stack Overflow用户
提问于 2016-12-06 23:21:53
回答 1查看 87关注 0票数 0

我试着把我的student_id连接到submission_id上,但是它不工作。我有一个连接表,叫做submissionstudent。

学生Controller.rb:

代码语言:javascript
复制
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

提交Controller.rb:

代码语言:javascript
复制
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:

代码语言:javascript
复制
class Student < ActiveRecord::Base
  has_many :submissionstudent
  has_many :submissions, :through => :submissionstudent
end

模型Submission.rb:

代码语言:javascript
复制
class Submission < ActiveRecord::Base
  belongs_to :form

  has_many :submissionstudent
  has_many :students, :through => :submissionstudent
end

Join表模型Studentsubmission.rb:

代码语言:javascript
复制
class Submissionstudent < ActiveRecord::Base
  belongs_to :submission
  belongs_to :student
end

如果您需要更多代码,请告诉我,谢谢您的帮助

EN

回答 1

Stack Overflow用户

发布于 2016-12-06 23:33:43

您可能需要根据最佳实践更改association_name、table_name、model_name和file_names,我确信它会起作用

关联模型名称:单数StudentSubmission

  • Model名称例如student_submission

  • Table snake_case :单数关联(_ )例如student_submission
  • Table名称:复数表名:复数例如students_submissions #表名称按字母顺序排在第一位提交关联:复数例如student_submissions
  • belongs_to submission

has_one CamelCase :单数例如submission

student.rb:

代码语言:javascript
复制
class Student < ActiveRecord::Base
  has_many :student_submissions
  has_many :submissions, :through => :student_submissions
end

submission.rb:

代码语言:javascript
复制
class Submission < ActiveRecord::Base
  belongs_to :form

  has_many :student_submissions
  has_many :students, :through => :student_submissions
end

student_submission.rb:

代码语言:javascript
复制
class StudentSubmission < ActiveRecord::Base
  # join_table_name: students_submissions
  belongs_to :submission
  belongs_to :student
end

此外,您还需要在使用关联的地方更改关联

代码语言:javascript
复制
@student_submission = @submission.student_submissions.build
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40998831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档