Wrong number of arguments error

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”) %>

<%= puts(user.full_name()) %>

and in routes:
map.root :controller => ‘users’

I get error:
ArgumentError in UsersController#index

wrong number of arguments (0 for 2)

Thanks for response.

John M. 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.

<%= puts(user.full_name()) %>

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
[email protected]

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.

John M. 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
[email protected]

On Nov 29, 7:34pm, John M. [email protected] 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