Undefined Method

Where can I define the method ‘tbluser’ in this error?

NoMethodError in Tblregisteredphone#list

Showing app/views/tblregisteredphone/list.rhtml where line #23 raised:

undefined method `tbluser’ for #Tblregisteredphone:0x375c778

Extracted source (around line #23):

20:
21:
22:


23: <%= link_to tblregisteredphone.tbluser.txtForename,
24: :action => “list”,
25: :tbluser => “#{tblregisteredphone.tbluser.txtForename}” %>
26:

The tblregisteredphones model and controller are as follows:

Model:
class Tblregisteredphone < ActiveRecord::Base
set_table_name “tblregisteredphones”
belongs_to :user,
:foreign_key => “intUserID”
set_primary_key(:TblRegisteredPhonesID)
end

Controller:
class TblregisteredphoneController < ApplicationController
layout “standard-layout”
scaffold :tblregisteredphone

def delete
	Tblregisteredphone.find(@params['id']).destroy
	redirect_to :action => 'list'
end

def create
	@tblregisteredphone =
                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

end

Thank you,

Alana

It would help to see the model and schema for the user table, which is
where the problem seems to be.

I notice that you say “belongs_to :user” and then you try to reference
tblregisteredphone.tbluser.txtForename, though. Shouldn’t it be
tblregisteredphone.user.txtForename instead? Also, is the user table
called users or tblusers, and how is it referred to in its model?

You really should name things more consistently. It’s not a good sign
when you’re confused by your own naming convention. If these are legacy
tables and you’re aliasing them in the models to simpler names, you
should pick one way to do it and stick to it throughout to prevent more
problems like this, nu?

Alana wrote:

Where can I define the method ‘tbluser’ in this error?

NoMethodError in Tblregisteredphone#list

Showing app/views/tblregisteredphone/list.rhtml where line #23 raised:

undefined method `tbluser’ for #Tblregisteredphone:0x375c778

Extracted source (around line #23):

20:
21:
22:


23: <%= link_to tblregisteredphone.tbluser.txtForename,
24: :action => “list”,
25: :tbluser => “#{tblregisteredphone.tbluser.txtForename}” %>
26:

The tblregisteredphones model and controller are as follows:

Model:
class Tblregisteredphone < ActiveRecord::Base
set_table_name “tblregisteredphones”
belongs_to :user,
:foreign_key => “intUserID”
set_primary_key(:TblRegisteredPhonesID)
end

Controller:
class TblregisteredphoneController < ApplicationController
layout “standard-layout”
scaffold :tblregisteredphone

def delete
Tblregisteredphone.find(@params[‘id’]).destroy
redirect_to :action => ‘list’
end

def create
@tblregisteredphone =
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
end

Thank you,

Alana

Hi Steve,

My mistake, it was meant to say “belongs_to :tbluser”, which I’ve
changed in the model and am now getting a slightly different error as
below.

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

Extracted source (around line #23):

20:
21:
22:


23: <%= link_to tblregisteredphone.tbluser.txtForename,
24: :action => “list”,
25: :tbluser => “#{tblregisteredphone.tbluser.txtForename}” %>
26:

The users table is called “tblusers” and the model is as follows:

class Tbluser < ActiveRecord::Base
set_table_name “tblusers”
has_many :tblregisteredphones
set_primary_key(:TblUsersID)
end

I am using legacy tables and am new to rails so am finding it difficult
to understand how the aliases work. Once I’ve set the aliases, can I
then refer to them everywhere else (controller and rhtml files) as as
the alias name? I had no problems creating this database as a test using
the normal rails naming conventions, but since using the legacy schema,
I have been unable to get it to work properly.

Thank you for your help, it is much appreciated.

Alana