Hey all, I did a script/generate controler users in console. Then I added the following to users controller: class UsersController < ApplicationController def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def full_name() return "#{@first_name} + #{@last_name}" end end I have this in users/index.html.erb <% user = UsersController.new("John", "Merlino") %> <h1><%= puts(user.full_name()) %></h1> and in routes: map.root :controller => ''users'' I get error: ArgumentError in UsersController#index wrong number of arguments (0 for 2) Thanks for response. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino wrote in post #964838:> Hey all, > > I did a script/generate controler users in console. > > Then I added the following to users controller: > > class UsersController < ApplicationController > def initialize(first_name, last_name) > @first_name = first_name > @last_name = last_name > end > > def full_name()You don''t need the empty parentheses.> return "#{@first_name} + #{@last_name}" > end > end >No! These methods belong in the model, not the controller. Review MVC philosophy: a model object represents data. The controller mediates between the model and the view.> I have this in users/index.html.erb > > <% user = UsersController.new("John", "Merlino") %>Again, you want User, not UsersController.> > <h1><%= puts(user.full_name()) %></h1> > > and in routes: > map.root :controller => ''users'' > > I get error: > ArgumentError in UsersController#index > > wrong number of arguments (0 for 2) > > Thanks for response.Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You are right. It worked. Nevertheless, the behavior was the same. We instantiated an object and called a constructor method of the class. Both User and UsersController are classes. I don''t know what is causing the behavior to be different. Are there any good books on MVC design patterns? I already have the book Design Patterns in Ruby and I don''t see MVC mentioned in it. Thanks for response. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 29, 7:34 pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> You are right. It worked. Nevertheless, the behavior was the same. We > instantiated an object and called a constructor method of the class. > Both User and UsersController are classes. I don''t know what is causing > the behavior to be different.Rails creates an instance of your controller for you as part of the request handling process, and is probably expecting a controllers initialize method to take 0 arguments, but since you''ve changed the signature of initialize that blows up. Overriding initialize like that on a subclass of ActiveRecord will also cause trouble but it might take a little longer for you to get into trouble. Fred>Are there any good books on MVC design > patterns? I already have the book Design Patterns in Ruby and I don''t > see MVC mentioned in it. Thanks for response. > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino wrote in post #964846:> You are right. It worked. Nevertheless, the behavior was the same. We > instantiated an object and called a constructor method of the class. > Both User and UsersController are classes. I don''t know what is causing > the behavior to be different.Gee, do you think it could have something to do with the fact that they inherit from different parents -- which could change the behavior?> Are there any good books on MVC design > patterns? I already have the book Design Patterns in Ruby and I don''t > see MVC mentioned in it. Thanks for response.Any basic Rails reference (including the Guides) explains how Rails uses MVC. You probably don''t need Design Patterns in Ruby for *anything* at this stage... Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.