Forum: Ruby on Rails Initialize, instance variable....wrong number of arguments!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F7aaedb7942594057563a5c48892c8b0?d=identicon&s=25 Alex Sörensen (alexsoerensen)
on 2006-05-14 05:50
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
....
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-05-14 06:07
(Received via mailing list)
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 !
F7aaedb7942594057563a5c48892c8b0?d=identicon&s=25 Alex Sörensen (alexsoerensen)
on 2006-05-14 06:45
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 !
This topic is locked and can not be replied to.