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: </font> 21: </td> 22: <td> 23: <%= link_to tblregisteredphone.tbluser.txtForename, 24: :action => "list", 25: :tbluser => "#{tblregisteredphone.tbluser.txtForename}" %> 26: </td> 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 -- Posted via http://www.ruby-forum.com/.
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: </font> > 21: </td> > 22: <td> > 23: <%= link_to tblregisteredphone.tbluser.txtForename, > 24: :action => "list", > 25: :tbluser => "#{tblregisteredphone.tbluser.txtForename}" %> > 26: </td> > > 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-- Posted via http://www.ruby-forum.com/.
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: </font> 21: </td> 22: <td> 23: <%= link_to tblregisteredphone.tbluser.txtForename, 24: :action => "list", 25: :tbluser => "#{tblregisteredphone.tbluser.txtForename}" %> 26: </td> 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 -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
On Apr 24, 2006, at 13:26, Alana Murphy wrote:> 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>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
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 Noria 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-- Posted via http://www.ruby-forum.com/.
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:>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 Noria 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 > > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails_Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.