我收到以下错误...
pry("serp")> session[self.to_sym] = "closed"
NameError: undefined local variable or method `session' for "serp":String
...当我尝试在String类的monkeypatch中设置会话变量时。 (必要时我可以跟踪延迟工作中的工作进度,以便在他们准备好时加载我的搜索结果。)
如何在那里设置会话变量?或者有更好的解决方案吗?
我的代码......
/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
** /应用/视图/布局/ 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
答案 0 :(得分:0)
将会话作为参数传递。
class String
def multisearch session
result = PgSearch.multisearch(self)
session[self.to_sym] = "closed"
return result
end
end
然后
@term.delay.multisearch(session)
答案 1 :(得分:0)
答案是停止对抗Ruby的OO性质并构建一个可以拥有我需要访问的所有内容的搜索模型。
<强> /app/models/search.rb 强>:
class Search < ActiveRecord::Base
serialize :results
def multisearch
results = PgSearch.multisearch(self.term).to_a
self.update_attributes :results => results, :status => "closed"
return results
end
end
** /app/controllers/searches_controller.rb **:
class SearchesController < ApplicationController
respond_to :html
def show
if params[:term].present?
@search = Search.find_or_create_by_term(params[:term])
if @search.status.blank?
@search.delay.multisearch
@search.status = "open"
@search.save
elsif @search.status == "closed"
@search.update_attributes :status => nil
end
end
end
end
<强> /app/views/searches/show.html.haml 强>:
- if @search.present? && @search.status == "open"
# progress bar etc
- else
= form_tag search_path, :method => :get do
= text_field_tag "term", "Search term"
= submit_tag "Search"
- if @search.present? && @search.status.nil?
- unless @search.results.blank?
# show the search results
<强> /app/views/layouts/application.html.haml 强>:
- if @search.present? && @search.status == "open"
%meta{:content => "5", "http-equiv" => "refresh"}/