2013-03-27 65 views
0

,我發現了以下錯誤monkeypath內...如何設置會話變量從字符串

pry("serp")> session[self.to_sym] = "closed" 
NameError: undefined local variable or method `session' for "serp":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 

** /應用/視圖/佈局/ 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

答案是停止戰鬥Ruby的面向對象的性質和構建可以擁有我需要的一切訪問的搜索模式。

/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 

/應用/視圖/搜索/ 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"}/ 
0

傳遞會話作爲參數。

class String 
    def multisearch session 
    result = PgSearch.multisearch(self) 
    session[self.to_sym] = "closed" 
    return result 
    end 
end 

然後

@term.delay.multisearch(session) 
+0

謝謝!這恐怕不行。我決定停止與OO對抗,並開始構建搜索模型來擁有所有這些東西。當我得到它的工作,我會回來發佈一個答案。但首先我必須修復一個痛苦的序列化問題:http://stackoverflow.com/questions/15671773/why-cant-i-save-an-object-with-a-serialized-attribute – 2013-03-28 00:18:41