I’m new to rails and have stumbled across this error. I’m not sure where
I am supposed to define the method user_id . I have included the
offending code and my controller. Can anyone help?!
NoMethodError in Registeredphone#new
Showing app/views/registeredphone/new.rhtml where line #15 raised:
undefined method `user_id’ for #Registeredphone:0x3874030
This error message means that a method “user_id” is being called on
an instance of the class Registeredphone, and such a method is
unknown by instances of said class. The trace suggests the offending
code is around line 15 of “app/views/registeredphone/new.rhtml”.
15: <%= ‘selected’ if user.id == @registeredphone.user_id %>>
Yeah, right there. You need to decide whether that should be a valid
call, and if it should why the method is actually not defined.
As I’m new to rails and still learning my way, I’m not sure why the
method is not defined or where it should be defined. My model is as
follows:
class Registeredphone < ActiveRecord::Base
set_table_name “tblregisteredphones”
set_primary_key “TblRegisteredPhonesID”
belongs_to :user, :foreign_key => “intUserID”
end
Is this where I should be defining ‘user_id’? The foreign key in this
table (intUserID)is linked to the id in the user table (TblUserID) and
has been aliased due to difficult naming conventions used in this legacy
schema. Do I need to do more, or is this alias method correct?
Thank you,
Alana
Xavier N. wrote:
On Apr 24, 2006, at 13:26, Alana Murphy wrote:
undefined method `user_id’ for #Registeredphone:0x3874030
This error message means that a method “user_id” is being called on
an instance of the class Registeredphone, and such a method is
unknown by instances of said class. The trace suggests the offending
code is around line 15 of “app/views/registeredphone/new.rhtml”.
15: <%= ‘selected’ if user.id == @registeredphone.user_id %>>
Yeah, right there. You need to decide whether that should be a valid
call, and if it should why the method is actually not defined.
ActiveRecord is expectinb your registered_phone table to contain a
column named ‘user_id’. It seems you are using ‘intUserID’ to keep
track of the relationship, so if you change ‘user_id’ in your view to
‘intUserID’, it should work.
On Monday, April 24, 2006, at 2:22 PM, Alana Murphy wrote:
On Apr 24, 2006, at 13:26, Alana Murphy wrote:
call, and if it should why the method is actually not defined.