I’m getting the following error and have checked and double checked all
my table and field names are correct. Only the field names have
uppercase letters in them. Where am I going wrong?
NoMethodError in Tblregisteredphone#list
Showing app/views/tblregisteredphone/list.rhtml where line #23 raised:
You have a nil object when you didn’t expect it!
The error occured while evaluating nil.txtForename
Are you sure you have an object tblregisteredphone that has data? If
that
is one of your models, you need to create an instance of that model.
Might
try putting <%= debug tblregisteredphone %> in your view to see if the
object is being created. I would need to see what the controller is
doing
to offer some more help.
I created a test database using the conventional rails naming such as
“id” for primary key and “tblname_id” for foreign keys. I then recreated
the rails database for an existing database and had to use their primary
and foreign key names. Since then, even though I have set the primary
and foreign keys in the table models, I have been getting errors such as
this no method error.
Tblregisteredphone.new(@params[‘tblregisteredphone’]) @tblregisteredphone.txtregisterdatetime = Time.now
if @tblregisteredphone.save
redirect_to :action => ‘list’
else
render_action ‘new’
end
end
def new
@tblregisteredphone = Tblregisteredphone.new
@tbluser = Tbluser.find_all
end
def list
@tbluser = @params['tbluser']
@tblregisteredphones = Tblregisteredphone.find_all
end
def edit
@tblregisteredphone = Tblregisteredphone.find(@params["id"])
@tbluser = Tbluser.find_all
end
The list method doesn’t initialize tblregisteredphone. Is that
initialized somewhere in your view before the exception?
Also, your create method doesn’t initialize tbluser_id.
Do all of your entries in tblregisteredphone have non-null tbluser_id?
This line is in my view:
<% @tblregisteredphones.each do |tblregisteredphone| %>
Is this correct?
How do I initialize intUserID (the foreign key in tblregistered
phones) and is this the correct naming convention?
My tables are as follows:
“tblusers” - primary key “TblUsersID”
“tblregisteredphones” - primary key “TblRegisteredPhonesID”
- foreign key “intUserID”