Friday, July 23, 2010

SessionStore in Rails

Recently, I am learning how to use Rails( the version is 2.3.5,ruby version is 1.8.6). I need to use session  in Rails。Considering about the scalability, we should store the session to database(MySQL 5.x). The default value of session_store in Rails is File, so we need to modify the configuration of environment.rb file which is located in config foler. The setting is:

config.action_controller.session_store = :active_record_store

But, unexpected progress is in the matter, when I modified the setting value, the website can not be open(Firefox), the error page is as below:imageI know it must be session issue. After checking the session table in database and the configuration of session in rails, no problem was found. But when I checked the log, I found the following error information:

Status: 500 Internal Server Error
  Mysql::Error: Data too long for column 'session_id' at row 1: INSERT INTO `sessions` (`updated_at`, `session_id`, `data`, `created_at`) VALUES('2010-07-23 01:17:05', 'BAh7CDoPc2Vzc2lvbl9pZCIlMDNlNTg0ZDg2NjJkZmFjZTdiMjUwN2ExNmYyNGNjNjAiCmZsYXNoSUM6J0FjdG
lvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA6EF9jc3JmX3Rva2VuIjFWNStxMUc
4bThmUmJHQ3VHRGQ3Q2RJR2p1aStUejdtRmRtc0xJY1RIOUNrPQ==--6f29ea421cd0f14d5e8de70935515c26021c254f', 'BAh7AA==\n', '2010-07-23 01:17:05')

Obviously, the crux of problem is clearly that the session_id is too long instead of configuration of session. So I checked the columns of sessions table. Its definition is:

image However, I had setted the value of secret of session, like this:

config.action_controller.session = {
  :key => '_session',
  :secret      => 'f914e9b1bbdb829688de8512f5fea7d8e83fb35bfe2b56bcf1e6438d1d1b7837c532f8c2ece2a2
d0e37812e9b210824089b1810a4e238a61dfd922dc9dd62521'
}

Despite I changed it into shorter value, the error is still.

Finnally, I found the answer.It is incredible but simple. That is clearing cookie in the explorer. That's OK! Now, let's query the session table:

image

The length of session_id becomes normal and the system becomes normal too. It is really a trap that is tend to be ingored. Mark it!

No comments: