1

I am using Ruby on Rails and for some reason when I try to store a hash in my session it doesn't transfer across pages, however the test variable does. The code is as follows:

class HomeController < ApplicationController
require 'read_file'

def index
    session[:test] = 1
end

def uploadDictionary
    #post = ReadFile.read(params['dictionary'])
    file = params['dictionary']
    wordHash = Hash.new(0)

    file.read.each_line  do |line|
        wordHash[line.downcase] = line.downcase
        #session[:hmm] = line
        #puts session[:hmm]
    end

    #wordHash.each { |key, value| puts key + ' = ' + value + "\n" }     
    session[:storedDictionary] = wordHash
    #session[:storedDictionary].each { |key, value| puts key + ' = ' + value + "\n" }
    puts session[:test]
    redirect_to :action => "index"
end

def checkWord
    session[:storedDictionary].each { |key, value| puts key + ' = ' + value + "\n" }
    #puts session[:storedDictionary].key(params[:submittedWord].downcase)
    puts session[:test]
    render :text => "woot"
end
end

The issue is session[:storedDictionary] doesn't maintain itself across uploadDictionary and checkWord, I am hoping someone could give me some advice. Thanks. Update to show my view code:

<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>

<%= form_tag({:action => :uploadDictionary}, :multipart => true) do %>
  <%= file_field_tag 'dictionary' %>
  <%= submit_tag "Upload" %>
<% end %>
<br>
<br>
<%= form_tag("/checkWord", :method => 'get') do %>
  <%=  text_field_tag 'submittedWord' %>
  <%= submit_tag "checkWord" %>
<% end %>
Jeff_Hd
  • 2,174
  • 3
  • 19
  • 29

1 Answers1

0

You probably need to create the session db and enable sessions. see this question:

Rails sessions current practices

Community
  • 1
  • 1
Joshua Smith
  • 6,561
  • 1
  • 30
  • 28
  • Unfortunately I have already done that (retried it just in case though!) and it hasn't solved the issue. Note that it works with session[:test] just not session[:storedDictionary] – Jeff_Hd May 09 '12 at 00:24
  • and the client you are hitting both services with accepts cookies correctly (for the session cookie)? – Joshua Smith May 09 '12 at 00:35
  • Yeah it looks like it, it only seems to happen when redirecting from uploadDictionary back to index. – Jeff_Hd May 09 '12 at 00:42
  • I don't see your index code, is it possible you are resetting the users session at some point on/in/near index? – Joshua Smith May 09 '12 at 00:45
  • I've added my index view and I only have session[:test=1] in the controller for now - just hoping to get some basics up and running before playing around – Jeff_Hd May 09 '12 at 00:48