I’m using this in almost all methods of my Customer Controler
if params[:type]==‘in’
# handle incoming customers
kind = ::KIND_CUSTOMER
else
# handle outgoing customers
kind = ::KIND_SUPPLIER
end
I don’t want to copy&paste in all of the methods of this control, so I
tried to put it at the top of the controller, to be available to all
methods, but it doesn’t work …
This is what I want, but it doesn’t work:
class CustomerController < ApplicationController
around_filter :login_required
if params[:type]==‘in’
# handle incoming customers
kind = ::KIND_CUSTOMER
else
# handle outgoing customers
kind = ::KIND_SUPPLIER
end
erro => undefined local variable or method `params’ for
CustomerController:Class
I tried to put in a helper, but the helper’s methods are not recognized
in the controller …
add a before_filter that does this (you’ll want to use an instance
variable for kind instead of a local variable
Fred
ok, I added this to the customer controll:
def get_kind
if params[:type]=='in'
# handle incoming customers
@kind = ::KIND_CUSTOMER
else
# handle outgoing customers
@kind = ::KIND_SUPPLIER
end
end
and at the top, just:
before_filter :get_kind
and where I need it, I just put @kind
tahnks, it’s working, what I don’t understand, is why I have to create a
method and call it with a before_filter, instead of simply adding the
code at the top …
variable for kind instead of a local variable
else
and where I need it, I just put @kind
tahnks, it’s working, what I don’t understand, is why I have to
create a
method and call it with a before_filter, instead of simply adding the
code at the top …
‘Adding code’ at the top tries to execute the code in the context of
the controller class.
Code added at the top level is run when the controller is loaded, ie
once only - (in production classes aren’t reloaded). You need
something that happens once per request, is run by an instance of the
controller at a particular point in time (after the request has been
received by the controller, before the action code is run), so it
can’t possibly be achieved by class level stuff
‘Adding code’ at the top tries to execute the code in the context of
the controller class.
Code added at the top level is run when the controller is loaded, ie
once only - (in production classes aren’t reloaded). You need
something that happens once per request, is run by an instance of the
controller at a particular point in time (after the request has been
received by the controller, before the action code is run), so it
can’t possibly be achieved by class level stuff
Fred
thanks, I understand now …
rai
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.