我有一个 before_filter 确保session[:event_id]
存在并重定向到,select_event
如果它不存在。 select_event
向 发送 ajax 调用choose_event_by_keycode
。 choose_event_by_keycode
然后设置session[:event_id]
并重定向到索引。问题是check_event_permissions
过滤器session[:event_id]
在调用时没有。怎么了?
before_filter :check_event_permission, :except => [:select_event, :choose_event_by_keycode]
def check_event_permission
puts "@!@@@@@@@@@@@@@@@@@@@@@@@"
puts session
puts "@!@@@@@@@@@@@@@@@@@@@@@@@"
redirect_to :action =>'select_event' if !session[:event_id]
end
def choose_event_by_keycode
@event = Event.find_by_keyCode(params[:keyCode])
if @event
current_user.add_to_event_as_patron(@event.id)
session[:event_id] = @event.id
puts "!!@@@@@@@@@@@@@@@@@@@@@@@"
puts session
puts "!!@@@@@@@@@@@@@@@@@@@@@@@"
redirect_to mobile_path
else
render 'select_event', :notice => "No event found with that key.", :layout => false
end
end
服务器日志(为清楚起见修改了空格,并移动了 puts 输出,以便它们按操作顺序显示):
Started GET "/mobile/choose_event_by_keycode?keyCode=12345" for 192.168.5.101 at 2012-05-01 12:29:53 -0400
Processing by MobileController#choose_event_by_keycode as */*
Parameters: {"keyCode"=>"12345"}
Event Load (0.1ms) SELECT `events`.* FROM `events` WHERE `events`.`keyCode` = '12345' LIMIT 1
User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
EventPatron Load (0.0ms) SELECT `event_patrons`.* FROM `event_patrons` WHERE `event_patrons`.`user_id` = 1 AND `event_patrons`.`event_id` = 1
!!@@@@@@@@@@@@@@@@@@@@@@@
{"session_id"=>"ea0a0930f7723719d5c29c722ab3d1ed", "_csrf_token"=>"jzhMJcs2lcsGc9LFZi5PhJbGRG1mTrznFeawWHdUOHk=", "user_id"=>1, "user_type"=>"super", "event_id"=>1}
!!@@@@@@@@@@@@@@@@@@@@@@@
Redirected to http://192.168.5.205:3000/mobile
Completed 302 Found in 259ms (ActiveRecord: 6.3ms)
Started GET "/mobile" for 192.168.5.101 at 2012-05-01 12:29:54 -0400
Processing by MobileController#index as */*
@!@@@@@@@@@@@@@@@@@@@@@@@
{"session_id"=>"ea0a0930f7723719d5c29c722ab3d1ed", "_csrf_token"=>"jzhMJcs2lcsGc9LFZi5PhJbGRG1mTrznFeawWHdUOHk=", "user_id"=>1, "user_type"=>"super"}
@!@@@@@@@@@@@@@@@@@@@@@@@
Redirected to http://192.168.5.205:3000/mobile/select_event
Filter chain halted as :check_event_permission rendered or redirected
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
从日志中您可以看到session[:event_id]
已成功设置choose_event_by_keycode
,但随后消失在check_event_permission
更新
我发现了问题。事实证明,只有在您注销并重新登录时才会发生这种情况。在我的注销功能中,我有:
reset_session
session[:user] = nil
不要问我为什么session[:user] = nil
会在那里。但是,我删除了它,一切正常。SOOOO,现在问题变成了Why did having session[:user] = nil cause the observed behavior?