救救我!我想在表Cydata中保存数据。但我错了
def new
@cydata = Cydata.new
end
错误
PG::UndefinedTable: ERROR: relation "cydata" does not exist LINE 5: WHERE a.attrelid = '"cydata"'::regclass ^ : SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '"cydata"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
模型
class Cydata < ActiveRecord::Base
belongs_to :course
belongs_to :year
end
class Year < ActiveRecord::Base
has_many :cydatas
has_many :courses, :through => :cydatas
end
class Course < ActiveRecord::Base
has_many :cydatas
has_many :years, :through => :cydatas
end
控制器
class CydatasController < ApplicationController
before_action :set_cydata, only: [:show, :edit, :update, :destroy]
def index
@cydatas = Cydata.all
end
def new
@cydata = Cydata.new
end
def create
@years = Year.all
@courses = Course.all
Cydata.transaction do
@cydata = Cydata.new(cydata_params)
respond_to do |format|
if @cydata.save?
format.html { redirect_to @cydata, notice: 'Cydata was successfully created.' }
format.json { render action: 'show', status: :created, location: @cydata }
else
format.html { render action: 'new' }
format.json { render json: @cydata.errors, status: :unprocessable_entity }
end
end
end
end
private
def set_cydata
@cydata = Cydata.find(params[:id])
end
def cydata_params
params.require(:cydata).permit(:year_id, :course_id, :kolvo_studentov)
end
end
答案 0 :(得分:0)