0

我对这个编程内容的想法如下:“adult”代表学生数据表,“adultclasses”代表选课数据表。

当数据键入人输入学生数据时,他们必须避免输入成人内容中不存在的student_id。但是,我尝试了几次仍然失败。

def create
    @adult_class = AdultClasse.new(params[:adult_classe])
    @adults_session_descriptions = AdultsSessionDescription.all
    if 
      @adult_class = @adult_class.find(:student_id) == Adult.find_by_student_id(:student_id)       
      flash[:notice] = "no this student id record!"
      redirect_to:action => :index       
    else   
         respond_to do |format|
        if @adult_class.save
          format.html { redirect_to(@adult_class, :notice => 'adult classe was successfully created.') }
          format.xml  { render :xml => @adult_class, :status => :created, :location => @adult_class }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @adult_class.errors, :status => :unprocessable_entity }
        end
      end
    end

因为我错误地使用复数名称来构建程序,所以我在adultclasse.rb 中编造如下:

# encoding: utf-8
class AdultClasse < ActiveRecord::Base
  set_table_name "adult_classes"
  belongs_to :adult
  #has_many :adults_classe_ship 
  #has_many :adult, :through => :adults_classe_ship 
  validates :student_id,  :presence =>  {:message => "please input student id"}, 
                          :length => {:message => "too short or too long", :minimum => 4, :maximum => 12}        
end

这是模型 Adult.rb

# encoding: utf-8
    class Aadult < ActiveRecord::Base  
    before_save :fix_cname

  def age
    now = Time.now.utc.to_date
    dob = self.birthday
    now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
  end

  private
  def fix_cname
   self.cname = self.cname.gsub(/[\w\s\b\$[:punct:]]/ , '')
  end      

  #has_many :adults_classe_ship, :dependent => :destroy
  #has_many :adult_classe, :through => :adults_classe_ship
4

0 回答 0