我正在使用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 %>