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.
Preethi Naveen (Guest)
on 2006-03-22 13: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
Andy Shen (Guest)
on 2006-03-22 13: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
Daniel -. (Guest)
on 2006-03-22 13: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
Dattatraya G. (Guest)
on 2006-03-22 14:23
(Received via mailing list)
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
Daniel -. (Guest)
on 2006-03-23 00: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.