Forum: Ruby on Rails undefined method

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.
Alana M. (Guest)
on 2006-04-24 15:26
Hi,

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>

Extracted source (around line #15):

12:   <select name="registeredphone[user_id]">
13:    <% @user.each do |user| %>
14:        <option value="<%= user.id %>"
15:          <%= 'selected' if user.id == @registeredphone.user_id %>>
16:          <%= user.txtForename %>
17:        </option>
18:    <% end %>

My controller is as follows:

class RegisteredphoneController < ApplicationController
	layout "standard-layout"
	scaffold :registeredphone

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

	def create
		@registeredphone = Registeredphone.new(@params['registeredphone'])
		@registeredphone.txtregisterdatetime = Time.now
		if @registeredphone.save
			redirect_to :action => 'list'
		else
			render_action 'new'
		end
	end

	def new
		@registeredphone = Registeredphone.new
		@user = User.find_all
	end

	def list
		@user = @params['user']
		@registeredphones = Registeredphone.find_all
	end

	def edit
		@registeredphone = Registeredphone.find(@params["id"])
		@user = User.find_all
	end
end

Any help would be appreciated!!
Alana
Xavier N. (Guest)
on 2006-04-24 15:35
(Received via mailing list)
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.

-- fxn
Alana M. (Guest)
on 2006-04-24 16:22
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.
>
> -- fxn
Kevin O. (Guest)
on 2006-04-24 16:46
(Received via mailing list)
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.
>>
>> -- fxn
>
>
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>removed_email_address@domain.invalid
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
Alana M. (Guest)
on 2006-04-24 19:48
Thanks for your help, its all up and running now!

Alana
This topic is locked and can not be replied to.