我想在表Cydata中保存数据。但我在@cydata = Cydata.new上犯了一个错误

时间:2014-04-27 19:53:41

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord ruby-on-rails-4

救救我!我想在表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

1 个答案:

答案 0 :(得分:0)

您需要生成并填充迁移。请查看有关此问题的guide

$ rails generate migration createCydata

然后编辑生成的文件并运行:

$ rake db:migrate