Forum: Ruby on Rails No Method error

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.
434eecf2bd6df521bfd599e403be9996?d=identicon&s=25 Alana (Guest)
on 2006-04-07 14:59
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:    </font>
21:    </td>
22:    <td>
23:      <%= link_to tblregisteredphone.tbluser.txtForename,
24: 		 :action => "list",
25: 		 :tbluser => "#{tblregisteredphone.tbluser.txtForename}" %>
26:    </td>

Alana
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstein (Guest)
on 2006-04-07 18:08
(Received via mailing list)
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
434eecf2bd6df521bfd599e403be9996?d=identicon&s=25 Alana (Guest)
on 2006-04-07 18:25
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
434eecf2bd6df521bfd599e403be9996?d=identicon&s=25 Alana (Guest)
on 2006-04-10 13:07
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
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-04-10 14:42
(Received via mailing list)
Alana wrote:

>
> Extracted source (around line #23):
>
> 20:    </font>
> 21:    </td>
> 22:    <td>
> 23:      <%= link_to tblregisteredphone.tbluser.txtForename,
> 24: 		 :action => "list",
> 25: 		 :tbluser => "#{tblregisteredphone.tbluser.txtForename}" %>
> 26:    </td>


Tblregisteredphone.tbluser is nil.

You didn't initialize Tblregisteredphone[tbluser_id].

What does your controller look like?

--

Ray
434eecf2bd6df521bfd599e403be9996?d=identicon&s=25 Alana (Guest)
on 2006-04-10 14:51
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
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-04-10 15:01
(Received via mailing list)
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
434eecf2bd6df521bfd599e403be9996?d=identicon&s=25 Alana (Guest)
on 2006-04-10 15:57
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
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-04-10 16:15
(Received via mailing list)
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
434eecf2bd6df521bfd599e403be9996?d=identicon&s=25 Alana (Guest)
on 2006-04-11 12:31
Ray Baxter 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
This topic is locked and can not be replied to.