是否可以根据会话值路由请求?
我想根据会话值路由/test
到test#withoutsession
或/test
到。test#withsession
您最好的选择可能是对您的路线应用动态约束,如下所示:
get '/test', :to => 'test#withoutsession', :constraints => lambda{ |req| req.session[:user_id].blank? }
get '/test', :to => 'test#withsession', :constraints => lambda{ |req| req.session[:user_id].present? }
参考:http ://edgeguides.rubyonrails.org/routing.html#advanced-constraints