If I put @fname = session[:user].first_name in the initialize method of
a controller, I get nil ojbect error. However, when I put the exact
same code in the template called by that controller (hard-coded
initialize to get past error) such as <%= session[:user].first_name %>
it works just fine.
Also, in the ‘Show Session Dump’ of the error page reporting the nil
object, the user attribute of ‘first_name’ is there
What would cause initialize of a controller to not be able to see
session variables?
Perhaps try putting a @ in front of session. But
(somebody correct me if I’m wrong), I don’t think vars
like @params, @request, @session are available in
initialize methods. I tried using @request.env in an
initialize method but got a nil object error, so
instead I put it in another method and added a
before_filter. If these vars aren’t available in
initialize methods, I’m curious why.
I was using @session before, but somebody on IRC told me that @session
has been deprecated in favor of just session. Either way, it doesn’t
work in initialize as is.
So, as you suggested, I created a before filter below and it works now:
before_filter :init
def init @domain = session[:user].domain_name
end
So, the bigger question for me now is why would @params, @request, @session not be available in initialize (as you found)? Anybody?
Perhaps try putting a @ in front of session. But
(somebody correct me if I’m wrong), I don’t think vars
like @params, @request, @session are available in
initialize methods. I tried using @request.env in an
initialize method but got a nil object error, so
instead I put it in another method and added a
before_filter. If these vars aren’t available in
initialize methods, I’m curious why.
I’m going through a few tutorials that I’d found online (and any extras
that anyone can suggest would be appreciated!). I’m looking at one which
is talking about using script/generate scaffold to actually generate the
files. When I run script/generate scaffold Todo, the files that are
generated are todos_controller.rb, and a number of files in views/todos.
I’m a little confused why it’s creating files in todos rather than todo
as the tutorials are showing. Any ideas?
I’m going through a few tutorials that I’d found online (and any
extras that anyone can suggest would be appreciated!). I’m looking
at one which is talking about using script/generate scaffold to
actually generate the files. When I run script/generate scaffold
Todo, the files that are generated are todos_controller.rb, and a
number of files in views/todos. I’m a little confused why it’s
creating files in todos rather than todo as the tutorials are
showing. Any ideas?
The tutorials are old: when the controller name is omitted, the
generator now pluralizes the model name by default.
For a singular name (or any other name), provide it explicitly:
script/generate scaffold Todo Todo
script/generate scaffold Todo QuePasaHombre
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)
So, the bigger question for me now is why would @params, @request, @session not be available in initialize (as you found)? Anybody?
The initialize method is used to … initialize the controller. It’s
where these instance variables are created for your use. Use
before_filters rather than override it.
Thanks for the clarification! Sorry, I’m a rails and ruby noob and it
was not obvious to me why session would be out of scope of the
initialize.
Further clarification, can I assume then that a before_filter actually
runs after the initialize method?
If yes, this makes me curious about how things flow. I am using
login_generator and I have before_filter :login_required, so if I wasn’t
logged in, this would imply that the initialize method for the current
controller runs even though I get redirected to the login page. Yes?
the current controller runs even though I get redirected to the
login page. Yes?
Thanks again for your valuable feedback!
Yup, you got it!
The controller is initialized with the request and response objects.
Then it processes the request, which begins with the before_filters,
then calls the appropriate action and renders the view, then calls
the after_filters.
So initialize is outside of the request processing chain.
Best,
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)
The controller is initialized with the request and response objects.
Then it processes the request, which begins with the before_filters,
then calls the appropriate action and renders the view, then calls
the after_filters.
So initialize is outside of the request processing chain.
Best,
jeremy
I have a related problem. When I try to access @params or params in a
before filter, I get a nil object error. I do not have an initialize
method.
So, the bigger question for me now is why would @params, @request, @session not be available in initialize (as you found)? Anybody?
The initialize method is used to … initialize the controller. It’s
where these instance variables are created for your use. Use
before_filters rather than override it.
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)