1
我有一個方法,我想要執行一些搜索邏輯,然後保存搜索對象具有搜索字符串和用戶標識的人進行搜索。Rails - 會話請求是零錯誤
搜索/保存的邏輯似乎是另有工作正常,但是當我試圖獲取當前用戶(使用的應用程序控制器的方法),它拋出了與會話做一個運行時錯誤:
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"}>
下面是搜索控制器的方法:
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
末
這裏的CURRENT_USER方法我想從我的應用程序調用C 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
...
末