Problem with params


#1

Hello,

I am trying to create a new record in a database by modifying the create
method generated by scaffold. I have a situation where i want to add new
student and while creating a new student, i am able to choose the
department
the student belongs to. However when i say create, i get an error saying
that Department expected, got string.

Parameters: {“commit”=>“Create”, “student”=>{“department”=>“1”}

the method

def create
@student = Student.new(params[:student]) #error caused at this line
if @student.save
:
end

The department field is of type integer
so it looks like it expects an integer value while department returns a
string. How do i convert it to an integer so that i can succesfully add
a
new student to the database

thanks
Naveen


#2

Hi,

I think even if you use integer you will still get similar error,
except it will say Fixnum instead of string.
Because it seems like :department should be of object Department, not
the foreign key.

Cheers,
Andy


#3

Hi,

In your model you could explicitly state the relationship

class Student < ActionRecord::Base
belongs_to :department, :class_name=“department”, :foriegn_key =>
‘department_id’
end

Then in the veiw use
<%= text_field “student”, “department_id” %>

or similar for a select box.

I had similar problems when I was trying to get a form working and found
that by doing this it worked. I think there is supposed to be some
rails
magic that does this for you but I don’t seem to have much luck with it.

Cheers


#4

Yes… It follows the belong_to requirements. But when you state is
specifically the field name could be any name of type int.


#5

is the foreign key department_id a field in the main table student?

On 3/22/06, Liquid removed_email_address@domain.invalid wrote:

Then in the veiw use

Andy
department
line



Dattatraya