Kalpesh1 Patel
2009-Nov-13 09:00 UTC
Wrong number of arguments (0 for 1) in callback method
I was creating this simple rail application. my controller method looks like this: ------- def create() @simple = Simple.new(params[:simple]) ##params coming from view @simple.save end ------- And my model class "Simple" as below: ------- class Simple < ActiveRecord::Base set_table_name "name_desc_table" set_primary_key "name" before_create :init_name private def init_name(cname) self.name = cname end end ------- 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? Thanks, -Kalpesh -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Nov-13 09:35 UTC
Re: Wrong number of arguments (0 for 1) in callback method
On Nov 13, 9:00 am, Kalpesh1 Patel <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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> > Thanks, > -Kalpesh > -- > Posted viahttp://www.ruby-forum.com/.
Kalpesh1 Patel
2009-Nov-13 09:39 UTC
Re: Wrong number of arguments (0 for 1) in callback method
Frederick Cheung wrote:> On Nov 13, 9:00�am, Kalpesh1 Patel <rails-mailing-l...@andreas-s.net> > wrote: > >> 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 > > FredSo isn''t there any way I can pass some value from controller to model? -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
Leonardo Mateo
2009-Nov-13 10:14 UTC
Re: Wrong number of arguments (0 for 1) in callback method
On Fri, Nov 13, 2009 at 10:39 AM, Kalpesh1 Patel <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Frederick Cheung wrote: >> On Nov 13, 9:00�am, Kalpesh1 Patel <rails-mailing-l...@andreas-s.net> >> wrote: >> >>> 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 > > 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 ~
Kalpesh1 Patel
2009-Nov-13 11:00 UTC
Re: Wrong number of arguments (0 for 1) in callback method
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'' > > > SijoThanks sijo! I am able to get/set it using attr_accessor now :) -- Posted via http://www.ruby-forum.com/.
Kalpesh1 Patel
2009-Nov-13 11:10 UTC
Re: Wrong number of arguments (0 for 1) in callback method
Leonardo Mateo wrote:> On Fri, Nov 13, 2009 at 10:39 AM, Kalpesh1 Patel > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 :) -Kalpesh -- Posted via http://www.ruby-forum.com/.
Matt Jones
2009-Nov-14 17:24 UTC
Re: Wrong number of arguments (0 for 1) in callback method
Somewhat unrelated, but if you''re creating a new, "simple" Rails app, why are you tangling with set_table_name and a noninteger primary key? Working against the Rails conventions will cause no end of trouble, especially if you''re just starting out... --Matt Jones On Nov 13, 4:00 am, Kalpesh1 Patel <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I was creating this simple rail application. my controller method looks > like this: > ------- > def create() > @simple = Simple.new(params[:simple]) ##params coming from view > @simple.save > end > ------- > > And my model class "Simple" as below: > > ------- > class Simple < ActiveRecord::Base > set_table_name "name_desc_table" > set_primary_key "name" > > before_create :init_name > > private > def init_name(cname) > self.name = cname > end > end > ------- > > 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? > > Thanks, > -Kalpesh > -- > Posted viahttp://www.ruby-forum.com/.
Kalpesh Patel
2009-Nov-15 12:31 UTC
Re: Wrong number of arguments (0 for 1) in callback method
Hi Matt, Actually I am trying to connect to already existing DB where mentioned table is readily available. I just need to read it all the time or occasionally write it but I do not create it from my app. The primary key of the table is varchar. I can do nothing about that and if i do not declare it into my model, it will assume a primary key ''id'' in the table. Is there any better way here? PS: i cannot see your posting on ruby-forum. Will it take LOTS of time for that to appear on the site or something else? -Kalpesh On Sat, Nov 14, 2009 at 10:54 PM, Matt Jones <al2o3cr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Somewhat unrelated, but if you''re creating a new, "simple" Rails app, > why are you tangling with set_table_name and a noninteger primary key? > Working against the Rails conventions will cause no end of trouble, > especially if you''re just starting out... > > --Matt Jones > > On Nov 13, 4:00 am, Kalpesh1 Patel <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> I was creating this simple rail application. my controller method looks >> like this: >> ------- >> def create() >> @simple = Simple.new(params[:simple]) ##params coming from view >> @simple.save >> end >> ------- >> >> And my model class "Simple" as below: >> >> ------- >> class Simple < ActiveRecord::Base >> set_table_name "name_desc_table" >> set_primary_key "name" >> >> before_create :init_name >> >> private >> def init_name(cname) >> self.name = cname >> end >> end >> ------- >> >> 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? >> >> Thanks, >> -Kalpesh >> -- >> Posted viahttp://www.ruby-forum.com/. > > >-- Why so serious?