On Mar 18, 10:03 pm, Stu <[email protected]> wrote: > When I try to view the page, I get the error: > Showing app/views/games/show.html.erb where line #8 raised: > > undefined method `get_player_name' for #<Class:0x252de68> > > Extracted source (around line #8): > > 5: > 6: <p> > 7: <b>Player2:</b> > 8: <%=h Player.get_player_name(@game.player2) %> > 9: </p> > 10: > 11: <p> > > I can call the get_player_name method from a player view. Clearly I > don't understand something about how rails does its scoping, but I am > not sure where to look. For instance, I don't see anything in my > Learning Rails book that might explain how this works. > > My question is: where can I put my get_player_name method so that it > can be accessed by other classes as well? >
This is more of a ruby / object oriented programming fundamentals question. You say that you defined this method in the players controller, but you're attempting to call it as if it was a class method on Player. If you wanted to make this work then you'd want to actually make it a class method on Player, ie you would write def.self.get_player_name(...) ... end in your player class. That would be slightly odd design though. A more railsy way of doing it would be for player2 to be an association and to define a full name method on Player ie def full_name firstName + lastName end and then your view would look like <%= h @game.player2.full_name %> It's also convention that attribute names be underscored (ie first_name rather than firstName) Fred -- 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

