会话不保存变量

时间:2012-05-09 00:07:52

标签: ruby-on-rails session session-variables

我正在使用Ruby on Rails,出于某种原因,当我尝试在我的会话中存储哈希时,它不会跨页面传输,但是测试变量会这样做。代码如下:

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

问题是session [:storedDictionary]没有在uploadDictionary和checkWord上维护自己,我希望有人可以给我一些建议。谢谢。更新以显示我的视图代码:

<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 %>

1 个答案:

答案 0 :(得分:0)

您可能需要创建会话数据库并启用会话。看到这个问题:

Rails sessions current practices