Just learning the framework. I apologize if this is in the worng forum. I looked around for something like "Beginner''s Questions" but didn''t see it. I''m building a simple application which will let players register for an event. So, I''ve generated a player_admin controller and pointed it to my players table. Worked great, I''m off and running. After learning about hashing the password, I decided I might like to add a convenience method to the Player (attached) which would give fullname. My ultimate goal of this is to list the players, obviously. In the player_admin/show view, I tried <%= @player.fullname %>, got an error which said fullname not defined. I also just dumped the player object using debug(), which didn''t show fullname as an attribute. I also tried it as a no-param method [i.e., fullname() ], without luck. After puzzling a while, I came across the notion of helpers, and decided to try the method in the player_admin_helper: def fullname @player.first_name + " " + @player.last_name end Worked like a charm. I kind of understand, it''s because the show view was invoked by the player_admin controller, so he has access to the player_admin_helper, but that won''t help me when I''m off in some other part of the application. I don''t understand where I went wrong when just trying to get a similar method directly in the player object. Is there something funadmentally wrong with how I''m thinking about Ruby objects, that in the Player model wasn''t really an appropriate place to try that? Thank you. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 28, 2008, at 1:51 PM, Scott Peterson wrote:> Just learning the framework. I apologize if this is in the worng > forum. > I looked around for something like "Beginner''s Questions" but didn''t > see > it. > > I''m building a simple application which will let players register > for an > event. > > So, I''ve generated a player_admin controller and pointed it to my > players table. Worked great, I''m off and running. > > After learning about hashing the password, I decided I might like to > add > a convenience method to the Player (attached) which would give > fullname. > My ultimate goal of this is to list the players, obviously. > > In the player_admin/show view, I tried <%= @player.fullname %>, got an > error which said fullname not defined. I also just dumped the player > object using debug(), which didn''t show fullname as an attribute. > > I also tried it as a no-param method [i.e., fullname() ], without > luck. > > After puzzling a while, I came across the notion of helpers, and > decided > to try the method in the player_admin_helper: > > def fullname > @player.first_name + " " + @player.last_name > endYou really want to have this in your Player model. It will look almost the same: class Player def fullname self.first_name + " " + self.last_name end end or perhaps: class Player def fullname "#{self.first_name} #{self.last_name}" end end> > > Worked like a charm. I kind of understand, it''s because the show view > was invoked by the player_admin controller, so he has access to the > player_admin_helper, but that won''t help me when I''m off in some other > part of the application. > > I don''t understand where I went wrong when just trying to get a > similar > method directly in the player object. Is there something > funadmentally > wrong with how I''m thinking about Ruby objects, that in the Player > model > wasn''t really an appropriate place to try that? > > Thank you.How did the method look when you tried to define it within your Player model? If you still tried to refer to @player rather than self, that may be your mental hurdle. You''re defining a new attribute of a model. The rest of the system shouldn''t care if the storage behind the model changes to hold the fullname and the other methods become: class Player def first_name self.fullname.split('' '').first end def last_name self.fullname.split('' '').last end end (NOTE: I''m not suggesting that this should happen, but only that the rest of the application can continue to reference @player.first_name, @player.last_name, or @player.fullname without having to know what really sits behind the model.) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn wrote:> On Nov 28, 2008, at 1:51 PM, Scott Peterson wrote: >> So, I''ve generated a player_admin controller and pointed it to my >> object using debug(), which didn''t show fullname as an attribute. >> end > You really want to have this in your Player model. It will look > almost the same: > > class Player > def fullname > self.first_name + " " + self.last_name > end > end > > or perhaps: > > class Player > def fullname > "#{self.first_name} #{self.last_name}" > end > end >Absolutely right. I want a single place which defines how I build "fullname", so when I add title, middle name, whatever, it just works everywhere I display.>> funadmentally >> wrong with how I''m thinking about Ruby objects, that in the Player >> model >> wasn''t really an appropriate place to try that? >> >> Thank you. > > How did the method look when you tried to define it within your Player > model? If you still tried to refer to @player rather than self, that > may be your mental hurdle. You''re defining a new attribute of a > model. The rest of the system shouldn''t care if the storage behind > the model changes to hold the fullname and the other methods become: > > class Player > def first_name > self.fullname.split('' '').first > end > def last_name > self.fullname.split('' '').last > end > end > > (NOTE: I''m not suggesting that this should happen, but only that the > rest of the application can continue to reference @player.first_name, > @player.last_name, or @player.fullname without having to know what > really sits behind the model.) > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgThanks a million for the response. Not sure what I did yesterday. I''m pretty sure I wasn''t referring to "@player", as I was in the Player model object. Some foolish typo, I imagine. Glad to hear that I had the right idea though. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---