Reputation: 1511
I am using Rails 3.2.13 with Ruby 1.9.3 and not using Devise.
I am writing script to reset session after each sign in. Please check below code and point out the issue.
Before using reset_session, everything is fine. I want to use reset_session feature.
Sessions controller
class SessionsController < ApplicationController
skip_before_filter :is_loggedin?, :only => [:new, :create]
def create
render :update do |page|
if !params[:email].blank? && !params[:password].blank?
if user = User.authenticate(params[:email], params[:password])
if user == "unuser"
page.alert("Your username and/or password was incorrect.")
elsif !user.account_id.blank?
temp_session = session.dup
reset_session
session.replace(temp_session)
# temp = session
# reset_session
# session.reverse_merge!(temp)
if params[:remember_me]
cookies.permanent[:user_id] = user.id
else
cookies[:user_id] = user.id
end
user.update_attributes(:last_login => Time.now, :is_online => true)
user.update_attributes(:time_zone => cookies["time_zone"])
if user.user_type=="teacher"
page.redirect_to home_teachers_path(:account_id =>user.account_id)
else
page.redirect_to home_students_path(:account_id =>user.account_id)
end
else
page.alert("You have not confirmed the activation link yet. Please check your registered email ")
end
else
page.alert("Invalid user/password.")
end
else
page.alert("You can't leave the fields empty.")
end
end
end
end
Below shows error logs:
Started POST "/sessions" for 127.0.0.1 at 2014-07-04 19:04:22 +0530
Processing by SessionsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"i123213232323233123+dfdfi/3424324324", "email"=>"[email protected]", "password"=>"[FILTERED]"}
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = '[email protected]' LIMIT 1
Completed 500 Internal Server Error in 20ms
NameError - undefined local variable or method `reset_session' for #<#<Class:0x000000068995a8>:0x000000075c44f8>:
app/controllers/sessions_controller.rb:26:in `block in create'
prototype-rails (3.2.1) lib/action_view/helpers/prototype_helper.rb:152:in `block in initialize'
actionpack (3.2.13) lib/action_view/helpers/capture_helper.rb:187:in `with_output_buffer'
prototype-rails (3.2.1) lib/action_view/helpers/prototype_helper.rb:151:in `initialize'
prototype-rails (3.2.1) lib/prototype-rails/renderers.rb:7:in `block in <module:Renderers>'
actionpack (3.2.13) lib/action_controller/metal/renderers.rb:35:in `block in _handle_render_options'
Upvotes: 1
Views: 747
Reputation: 84182
The problem is that you are doing all of this inside your render :update
block.
Inside that block self
is no longer the controller, which is why you can't call reset_session
(it's the controller's view context).
If I were you I'd try and disentangle the controller logic from the output (i.e. what is produced by render :update)
Upvotes: 2