1

I'm trying to develop a chatroom for my website. In turn, I'm using JQuery $.post to to send the message entered to pusher in my controller. I'm running into a strange error however. Every time I send a request via $.post my session is reset. I do not have any filters running which would reset the session. I have no filters which would reset the session in my application controller, so I am confused on what the problem is. Below you will find the code I have so far.

chat_functions.js

 $(document).ready(function() {

var text = 'Type your message here and hit enter...';
$('#message').focus(function() {
if($(this).val() == text) { $(this).val(""); }
}).blur(function() {
if($(this).val() == "") { $(this).val(text); }
});

$('#message').bind('keypress', function(e) {
    if(e.keyCode==13){
        if($('#message').val() == '') {
            alert('Please enter a message...');
            return false;
        } else {
            var message = $('#message').val();
            $.post('/send_chat_message', {"message":message }, function(response) {
                alert(response);
            });
        }
    }
});
 });

The message then gets sent to my controller so I can use pusher.

This is my controller code:

 class ChatController < ApplicationController

def index
    account = Account.getAccountById(session[:user])
    @title = "Chat"
end

def create
    Pusher['chat-channel'].trigger('send_message', params[:message])
    render :text => "success"
end

 end

Then chat.js displays the message. Here is the chat.js code:

 $(document).ready(function() {
// Enable pusher logging - don't include this in production
Pusher.log = function(message) {
    if (window.console && window.console.log) window.console.log(message);
};

// Flash fallback logging - don't include this in production
WEB_SOCKET_DEBUG = true;

var pusher = new Pusher('62651eca256339fa7fca');
var channel = pusher.subscribe('chat-channel');
channel.bind('send_message', function(payload) {
    $('#chatLog').append(payload);
});
 });

Now, when I refresh my page I am get the following exception: ActiveRecord::RecordNotFound

I get this exception because I am trying to find the account my session[:user] in my index method.

I am really confused on why my sessions are being reset. I figured it has to have something to do with $.post.

Any ideas on how I can prevent my sessions from being reset?

Thank you,

Brian

Brian
  • 5,951
  • 14
  • 53
  • 77

0 Answers0