on my site, I’m checking to see if the user has been authenticated. I have in my controller the following..
def index
login_required
@profile = current_user.profile ### line 9
..
Then in my application_controller, I have the definition of the login_required method..
def login_required
unless current_user
redirect_to root_path, :notice => "Please login first"
end
end
Even though my log shows me that the redirection to root is happening, it’s still not really taking me to the root path, and rather takes me back to my controller->index
and errors out on the line “@profile = current_user.profile”. here’s the log..
Started GET "/meetings" for 127.0.0.1 at 2012-12-05 12:18:12 -0800
Processing by xyzController#index as HTML
Redirected to http://localhost:3000/
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `profile' for nil:NilClass):
app/controllers/xyz_controller.rb:9:in `index'
Why am I not being redirected to the root path (which is my home page) even though it’s saying that it’s redirecting? Any ideas? Thanks.
Processing keeps happening after
redirect_to– there’s no implicitreturn. The band-aid fix is to add areturnin your index action depending on the result oflogin_required, but the better way is to use a filter in your controller.