No Method error

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

Extracted source (around line #23):

20:
21:
22:


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

Alana

Hi Alana ~

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.

~ Ben

Hi Ben,

tblregisteredphone definitely has data in it. The model for it is as
follows:

class Tblregisteredphone < ActiveRecord::Base
belongs_to :tbluser,

set_primary_key(:TblRegisteredPhonesID)
end

The model for the table it is related to (tbluser) is as follows:

class Tbluser < ActiveRecord::Base
has_many :tblregisteredphones

set_primary_key(:TblUsersID)

end

Thanks for your help,

Alana

Alana wrote:

Extracted source (around line #23):

20:
21:
22:


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

Tblregisteredphone.tbluser is nil.

You didn’t initialize Tblregisteredphone[tbluser_id].

What does your controller look like?

Ray

Hi Ray,

Tblregisteredphone.tbluser is nil.

You didn’t initialize Tblregisteredphone[tbluser_id].

What does your controller look like?

My tblregisteredphone controller is as follows:

class Tblregisteredphone < ActiveRecord::Base
    set_table_name "tblregisteredphones"
    belongs_to :tbluser,
       :foreign_key => "intUserID"
    set_primary_key(:TblRegisteredPhonesID)
end

“tbluser” has a primary key called “TblUsersID” and the foreign key in
“tblregisteredphones” is called “intUserID”.

Thanks for your help,

Alana

Hi,

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.

Can anyone please help me with this?

Alana

Sorry, the controller is as follows. What data do you need?

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

Alana

Alana wrote:

class Tblregisteredphone < ActiveRecord::Base
  set_table_name "tblregisteredphones"
  belongs_to :tbluser,
     :foreign_key => "intUserID"
  set_primary_key(:TblRegisteredPhonesID)
end

“tbluser” has a primary key called “TblUsersID” and the foreign key in
“tblregisteredphones” is called “intUserID”.

That’s your model. We need to see your controller, and probably your
data too.

Ray

Ray B. wrote:

  1. The list method doesn’t initialize tblregisteredphone. Is that
    initialized somewhere in your view before the exception?

  2. Also, your create method doesn’t initialize tbluser_id.

  3. Do all of your entries in tblregisteredphone have non-null tbluser_id?

  1. This line is in my view:
    <% @tblregisteredphones.each do |tblregisteredphone| %>
    Is this correct?

  2. 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”

  3. All of the entries have a non-null intUserID.

Thank you for your help,

Alana

Alana wrote:

Sorry, the controller is as follows. What data do you need?

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?

Ray

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs