外键只在一个表上工作,而不在另一个表上工作?

时间:2019-11-28 02:12:17

标签: ruby-on-rails

我有两个不同的表,一个Team表和一个Project表。每个表都有一个TeamType表的外键。我正在使用一种表格填充使用collection_select选择要具有外键的团队类型的每个表。这非常适合Team表,但是当我尝试填写Project表的表单时,即使模型和迁移是结构化的,也会给我一个错误,提示“团队类型必须存在”几乎一样。这里可能是什么问题? 这是我的迁移:

class CreateProjects < ActiveRecord::Migration[5.1]
  def change
    create_table :projects do |t|
      t.references :team_type, foreign_key: true
      t.string :project_name

      t.timestamps
    end
  end
end
class CreateTeams < ActiveRecord::Migration[5.1]
  def change
    create_table :teams do |t|
      t.references :team_type, foreign_key: true
      t.string :team_name
      t.integer :num_of_students

      t.timestamps
    end
  end
end

这是我的模特:

class Project < ApplicationRecord
  validates :project_name, length: {maximum: 35}, presence: true
  belongs_to :team_type
  # add_index :team
end
class Team < ApplicationRecord
  validates :team_name, length: {maximum: 40}, presence: true
  belongs_to :team_type
  # add_index :team_name
end

以下是ProjectTeam的HTML格式:

  <div class="field">
    <%= form.label :team_type_id %>
    <%= collection_select(:project, :team_type_id, TeamType.all,:id,:type_name) %>
  </div>
  <div class="field">
    <%= form.label :team_type_id %>
    <%= collection_select(:team, :team_type_id, TeamType.all,:id,:type_name) %>
  </div>

以下是控制器:

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]

  # GET /projects
  # GET /projects.json
  def index
    @projects = Project.all
  end

  # GET /projects/1
  # GET /projects/1.json
  def show
  end

  # GET /projects/new
  def new
    @project = Project.new
  end

  # GET /projects/1/edit
  def edit
  end

  # POST /projects
  # POST /projects.json
  def create
    @project = Project.new(project_params)

    respond_to do |format|
      if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /projects/1
  # PATCH/PUT /projects/1.json
  def update
    respond_to do |format|
      if @project.update(project_params)
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { render :show, status: :ok, location: @project }
      else
        format.html { render :edit }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    @project.destroy
    respond_to do |format|
      format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_project
      @project = Project.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
      params.require(:project).permit(:project_name, :team_id)
    end
end
class TeamsController < ApplicationController
  before_action :set_team, only: [:show, :edit, :update, :destroy]

  # GET /teams
  # GET /teams.json
  def index
    @teams = Team.all
  end

  # GET /teams/1
  # GET /teams/1.json
  def show
  end

  # GET /teams/new
  def new
    @team = Team.new
  end

  # GET /teams/1/edit
  def edit
  end

  # POST /teams
  # POST /teams.json
  def create
    @team = Team.new(team_params)

    respond_to do |format|
      if @team.save
        format.html { redirect_to @team, notice: 'Team was successfully created.' }
        format.json { render :show, status: :created, location: @team }
      else
        format.html { render :new }
        format.json { render json: @team.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /teams/1
  # PATCH/PUT /teams/1.json
  def update
    respond_to do |format|
      if @team.update(team_params)
        format.html { redirect_to @team, notice: 'Team was successfully updated.' }
        format.json { render :show, status: :ok, location: @team }
      else
        format.html { render :edit }
        format.json { render json: @team.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /teams/1
  # DELETE /teams/1.json
  def destroy
    @team.destroy
    respond_to do |format|
      format.html { redirect_to teams_url, notice: 'Team was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_team
      @team = Team.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def team_params
      params.require(:team).permit(:team_type_id, :team_name, :num_of_students)
    end
end

感谢您的帮助,谢谢!

0 个答案:

没有答案
相关问题