Forum: Ruby on Rails Problem with params

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6d432a544f913c22997fade1e51fa7be?d=identicon&s=25 Preethi Naveen (Guest)
on 2006-03-22 12:28
(Received via mailing list)
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
:<snip>
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
67c06429f18c528461f65fc80f5b8281?d=identicon&s=25 Andy Shen (Guest)
on 2006-03-22 12:54
(Received via mailing list)
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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-03-22 12:59
(Received via mailing list)
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
Ada41dd2cc7c8be6ec92ea663d3c1c7d?d=identicon&s=25 Dattatraya Gokhale (Guest)
on 2006-03-22 13:23
(Received via mailing list)
is the foreign key department_id a field in the main table student?

On 3/22/06, Liquid <has.sox@gmail.com> wrote:
> Then in the veiw use
>
> > Andy
> > department
> > line
> > >
> > _______________________________________________
>
>
>


--
Dattatraya
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-03-22 23:06
(Received via mailing list)
Yes...  It follows the belong_to requirements.  But when you state is
specifically the field name could be any name of type int.
This topic is locked and can not be replied to.