2011-05-13 6 views
1

일부 검색 논리를 실행하고 검색 한 문자열과 사용자 ID가있는 검색 개체를 저장하는 방법이 있습니다.Rails 세션 요청이 무효입니다.

검색은/논리를 저장 그렇지 않으면 잘 동작하는 것,하지만 난 (응용 프로그램 컨트롤러에서 방법을 사용하여) 현재 사용자를 얻을려고 할 때 세션과 관련이있다 런타임 오류가 발생합니다 :

을 나는 내 응용 프로그램의 C에서 호출 할 노력하고있어 CURRENT_USER 방법

class SearchController < ApplicationController 

... 

def factualsearch(search) 

    if search 

     searchquery = Search.new 

     # this causes the error 
     if current_user 
     searchquery.user = current_user 
     end 

     searchquery.search_string = search 

     searchquery.save 

     ... 

    end 

    @results 

end 

끝 여기

것 : 여기

ActionController::Metal#session delegated to @_request.session, but @_request is nil: #<SearchController:0x1038e32e0 @action_has_layout=true, @view_context_class=nil, @_status=200, @_headers={"Content-Type"=>"text/html"}> 
검색 컨트롤러의 방법입니다 ontroller :

여기
def current_user 
    return unless session[:user_id] 
    @current_user ||= User.find_by_id(session[:user_id]) 
end 

helper_method :current_user 

내가 방법을 호출하고있어 페이지 컨트롤러 :

class PagesController < ApplicationController 

... 

def search 

    searchcontrol = SearchController.new   

    @results = searchcontrol.factualsearch(params[:search]) 

end 

... 

끝에이 문제를 해결하기 위해

답변

0

정확히는 직접적인 방법은,하지만 난 주위에 얻을 수있었습니다 내부에서 current_user 메소드를 호출하는 대신 검색 쿼리 외에도 사용자가 함수를 받아들이도록해야합니다. 액션을 호출하는 클래스는 current_user 메소드에 액세스 할 수 있습니다.

관련 문제