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
...
末