params[:id]

hi there, i'm very new in ruby. i need to solve this asap. im out of idea already. please help me. thank u.

[code=]def egradebook end

def monthlytest #will find all record of person wif id of the current user (teacher) login @teachersubjects = Teachersubject.find(:all, :conditions => ["user_id = ?", session[:user_id]]) end

def monthlytestadd #here will list all student in class which has teacher of id 1001 @hello = Teachersubject.find(params[:id]) @students = Student.find(:all, :conditions => ["class1_id = ?", @hello.class1_id]) end

def addmark #Problem. now, the params[:id] has become class_id, so i cant take the teacher data (subject, year, semester) to be save in new gradebook record @teachersubject = Teachersubject.find(params[:id]) @students = Student.find(:all) @gradebook = Gradebook.new(params[:gradebook]) @gradebook.user_id = session[:user_id] @gradebook.subject_id = @teachersubject.subject_id #this part got prob @gradebook.year = @teachersubject.year #this part got prob @gradebook.semester = teachersubject.semester #this part got prob @gradebook.class1_id = params[:id] #and maybe need to change this part also? @gradebook.monthly = "yes" if @gradebook.save     flash[:notice] = 'Student Mark Successfully Added.'     redirect_to :action => 'monthlytest'     else    flash[:notice] = 'Error, Please Login'       render :action => 'login'       end end[/code]

i tried something like this which i think it could find the teacher's subject already but it turned error.

[code=]def addmark params['id'] = session[:user_id] @students = Student.find(:all) @teachersubject = Teachersubject.find(params[:id]) @gradebook = Gradebook.new(params[:gradebook]) @gradebook.user_id = session[:user_id] @gradebook.subject_id = @teachersubject.subject.id @gradebook.year = @teachersubject.year @gradebook.semester = @teachersubject.semester @gradebook.class1_id = @teachersubject.class1_id @gradebook.monthly = "yes" if @gradebook.save     flash[:notice] = 'Student Mark Successfully Added.'     redirect_to :action => 'monthlytest'     else    flash[:notice] = 'Error, Please Login'       render :action => 'login'       end end[/code] here's the error : ActiveRecord::RecordNotFound in AdminController#addmark Couldn't find Teachersubject with ID=1003

i want to get teacher's data from table teachersubject which consist of user_id, subject_id, class1_id, semester, year. but i dunno how to get those. maybe my params is wrong. 1003 is the teacher user_id

here are the data in the teachersubject table id -- subject_id -- user_id -- class1_id -- year -- semester 1 ----- 2 --------- 1003 --------- 1 ------ 2008 ----- 1 2 ----- 3 --------- 1003 --------- 2 ------ 2008 ----- 1

please give some ideas for me to solve this asap, thanks

Veil Yw wrote:

def monthlytest #will find all record of person wif id of the current user (teacher) login @teachersubjects = Teachersubject.find(:all, :conditions => ["user_id = ?", session[:user_id]]) end

Long term, you must learn to write cleaner code. Here are some general guidelines...

  - things with :conditions=> belong in the Models, not the controllers   - use _ between words in method names: def monthly_test   - use UpStyle in class names: TeacherSubject   - don't use @ on variables that shouldn't live longer than            the current method   - use automatically generated methods like           TeacherSubject.find_by_user_id(session[:user_id])   - A complex class name like "TeacherSubject" is a hint you          might need two models - Teacher and Subject!

@teachersubject = Teachersubject.find(params[:id])

ActiveRecord::RecordNotFound in AdminController#addmark Couldn't find Teachersubject with ID=1003

Yet that's what you asked for - if params[:id] contains 1003, and if teachersubject contains this...

id -- subject_id -- user_id -- class1_id -- year -- semester 1 ----- 2 --------- 1003 --------- 1 ------ 2008 ----- 1 2 ----- 3 --------- 1003 --------- 2 ------ 2008 ----- 1

...then find(id) will only find by teachersubject.id. You know that the params[:id] is really a user_id, but to find() it is only a number. Find() cannot see the :id part of params[:id], only the returned value.