Alex Sörensen
2006-May-14 03:50 UTC
[Rails] Initialize, instance variable....wrong number of arguments!
Dear RoR Community My new RoR experience went really smooth for some time until I got stuck with a seemingly easy problem and the more I have tried to read about it (PickAxe, Agile WD, forum, etc.) the more confused I got! What I have done so far as a newbie is to use the scaffold and then build my own ideas around it. What I am trying to do now is allow/disallow an action in the input form (in this case ''save'') depending on whether the ''status'' == WIP or not. However with the code below I keep getting error messages like ''wrong number of arguments (0 for 1)'' or ''Template is missing''. My 2 questions are: 1. Can anyone see what is wrong with my code? 2. Can anyone please give me some info or point me into the direction of where I can find info about instance variables, initialization for newbies like me? Thanks a lot, Alex class AdminController < ApplicationController ... def wipinput @job = Job.find(params[:id]) end def initialize(status) @status = status end def wipupdate if params[:save] and @status == ''WIP'' @job = Job.find(params[:id]) if @job.update_attributes(params[:job]) @job.update_attribute(:status,"WIP") flash[:notice] = ''Job was successfully updated.'' redirect_to :action => ''wip'' else render :action => ''wipinput'' end .... -- Posted via http://www.ruby-forum.com/.
Francois Beausoleil
2006-May-14 04:05 UTC
[Rails] Initialize, instance variable....wrong number of arguments!
Hello Alex, 2006/5/13, Alex S?rensen <as@hippolime.com>:> 1. Can anyone see what is wrong with my code?Yes, remove the definition of #initialize. That is overriding the method with the same name in ApplicationController or above.> 2. Can anyone please give me some info or point me into the direction of > where I can find info about instance variables, initialization for > newbies like me?Instance variables are initialized to nil when first referenced. If you need another value, in Rails, you could use a before filter. You have to remember that the controller instance will be recreated for each new request. This is the equivalent of calling AdminController.new.wipupdate for each request. Please bear in mind that this is not exactly how it works, but it is similar in concept. For your specific problem, you should look instead of storing your status in the user''s session, not in the controller itself. Hope that helps ! -- Fran?ois Beausoleil http://blog.teksol.info/
Alex Sörensen
2006-May-14 04:45 UTC
[Rails] Re: Initialize, instance variable....wrong number of argumen
Salut Francois, hello ror Thanks for the amazingly quick response! After deleting #initialize, the following if statement produces ''Template missing....wipupdate.rhtml'' even though is_status equals ''WIP'' and should therefore just return the usual response, i.e. redirect_to :action => ''wip'' which works fine if I leave out the second part of the if statement (and @is_status == ''WIP''). Any idea? Thanks a lot, Alex .... def wipupdate if params[:save] and @is_status == ''WIP'' ... Fran?ois Beausoleil wrote:> Hello Alex, > > 2006/5/13, Alex S?rensen <as@hippolime.com>: >> 1. Can anyone see what is wrong with my code? > > Yes, remove the definition of #initialize. That is overriding the > method with the same name in ApplicationController or above. > >> 2. Can anyone please give me some info or point me into the direction of >> where I can find info about instance variables, initialization for >> newbies like me? > > Instance variables are initialized to nil when first referenced. If > you need another value, in Rails, you could use a before filter. You > have to remember that the controller instance will be recreated for > each new request. > > This is the equivalent of calling AdminController.new.wipupdate for > each request. Please bear in mind that this is not exactly how it > works, but it is similar in concept. > > For your specific problem, you should look instead of storing your > status in the user''s session, not in the controller itself. > > Hope that helps !-- Posted via http://www.ruby-forum.com/.
Reasonably Related Threads
- Multiple actions from one form - possible?
- Problem with case / when conditional of db values
- [Bug 14393] New: NV11 card is not detected properly by nouveau on big-endian box
- Testing basic direct x capability
- [Bug 14567] New: Randr 1.2 fails on nv17 lvds in a Dell Inspiron 8100 ( continued from 14491)