# Params\[:id\]

**URL:** <https://www.ruby-forum.com/t/params-id/226001>\
**Category:** Rails\
**Created:** [March 26, 2008, 5:01am UTC](https://www.ruby-forum.com/t/params-id/226001 "2008-03-26T05:01:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![veil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a9a28c/32.png) [@veil](https://www.ruby-forum.com/u/veil)\
**Post date:** [March 26, 2008, 5:01am UTC](https://www.ruby-forum.com/t/params-id/226001/1 "2008-03-26T05:01:48Z")

</div>

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.

`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`  
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

---

<div class="post-metadata">

**Author:** ![veil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a9a28c/32.png) [@veil](https://www.ruby-forum.com/u/veil)\
**Post date:** [March 26, 2008, 9:24am UTC](https://www.ruby-forum.com/t/params-id/226001/2 "2008-03-26T09:24:36Z")

</div>

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.

–  
Phlip
