Wrong number of arguments (0 for 1) in callback method

When I run this in browser, it throws error "Wrong number of arguments (0 for 1)". Obviously, my method "init_name" expects one parameter and it doesn't find any while called(by controller??). Basically I want to send this 'cname' variable from controller to model. Can anybody tell me what can I do in controller method so that 'cname' variable is passed when callback method is called?

Callbacks like before_create can't take any arguments

Fred

Hi Kalpesh1 Patel

So isn't there any way I can pass some value from controller to model?

    You can define cname as n attr_writer in model and can set its value from controller like @simple.cname = 'namehere'

Sijo

What do you mean? Of course you can pass values, in fact, you're passing a value there. I don't see the need of doing something like the before_create hook. Why don't you just pass the value to the constructor? Simple.new(:name => params[:simple])

Sijo k g wrote:

Hi Kalpesh1 Patel

So isn't there any way I can pass some value from controller to model?

    You can define cname as n attr_writer in model and can set its value from controller like @simple.cname = 'namehere'

Sijo

Thanks sijo! I am able to get/set it using attr_accessor now :slight_smile:

Leonardo Mateo wrote:

when callback method is called?

Callbacks like before_create can't take any arguments

Fred

So isn't there any way I can pass some value from controller to model?

What do you mean? Of course you can pass values, in fact, you're passing a value there. I don't see the need of doing something like the before_create hook. Why don't you just pass the value to the constructor? Simple.new(:name => params[:simple])

-- Leonardo Mateo. There's no place like ~

I need before_create because I want to do some preprocessing everytime i save my 'Simple' state in the DB. For example, authentication, logging or similar stuff I will perform just before 'save'(this might raise another question, is this right approach? but this can be a separate discussion). I thought of passing the value in the callback method itself but now I am able to do it through attr_accessor. Thanks Leo for the suggestion :slight_smile:

-Kalpesh