我收到以下错误...
pry("serp")> session[self.to_sym] = "closed"
NameError: undefined local variable or method `session' for "serp":String
...当我尝试从 String 类的猴子补丁中设置会话变量时。(这是必要的,以便我可以跟踪延迟工作中的工作进度,以便仅在准备好时加载我的搜索结果。)
如何在那里设置会话变量?还是有更好的解决方案?
我的代码...
/config/initializers/string.rb:
class String
def multisearch
result = PgSearch.multisearch(self)
session[self.to_sym] = "closed"
return result
end
end
/app/views/searches/show.html.haml:
- if @term.present? && session[@term.to_sym] == "open"
%h1 Search In Progress
# then show spinning wheel animation etc
- else
%h1 Search Results
= form_tag search_path, :method => :get do
= text_field_tag "term", "Search term"
= submit_tag "Search"
- unless @results.blank?
# then show the search results etc
**/app/views/layouts/application.html.haml:
!!!
%html
%head
- if @term.present? && session[@term.to_sym] == "open"
%meta{:content => "5", "http-equiv" => "refresh"}/
/app/controllers/searches_controller.rb:
class SearchesController < ApplicationController
respond_to :html
filter_access_to :all
def show
if @term = params[:term]
session[@term.to_sym] = "open"
@results = @term.delay.multisearch
# other stuff...
end
end
end