2015-04-04 94 views
0

這使我瘋了。我使用的是Rails 3.2.17。我似乎無法在我的HomeController中使用Rails請求對象。NameError:未定義的本地變量或方法`請求'HomeController:類

我的HomeController代碼:

class HomeController < ApplicationController 
    respond_to :html, :json, :js 

    def refresh_blog 
    request.domain.include?('egg') ? (@blog_entries = Blog.fetch_entries(Blog::ED_BLOG_URL)) : (@blog_entries = Blog.fetch_entries(Blog::CS_BLOG_URL)) 
    respond_with(@blog_entries) do |format| 
     format.html { head :not_acceptable } 
    end 
    end 
end 

控制檯輸出採用撬:

22: # @blog_entries = Blog.fetch_entries 
23: # respond_with(@blog_entries) do |format| 
24: #  format.html { head :not_acceptable } 
25: # end 
26: # end 
=> 27: binding.pry 
28: def refresh_blog 
29:  request.domain.include?('egg') ? (@blog_entries = Blog.fetch_entries(Blog::ED_BLOG_URL)) : (@blog_entries = Blog.fetch_entries(Blog::CS_BLOG_URL)) 
30:  respond_with(@blog_entries) do |format| 
31:   format.html { head :not_acceptable } 
32:  end 

3.2.17 @ 2.1.0 (HomeController)>request 
NameError: undefined local variable or method `request' for HomeController:Class 
from (pry):2:in `<class:HomeController>' 
3.2.17 @ 2.1.0 (HomeController)>request.domain 
NameError: undefined local variable or method `request' for HomeController:Class 
from (pry):3:in `<class:HomeController>' 

可能是什麼回事?我所有的研究都表明請求對象應該在控制器和視圖中可用。

回答

1

binding.pry聲明似乎不在您粘貼的輸出中的refresh_blog方法AFAICT之外。

(和request上不HomeController類存在,但僅限於HomeController對象)

相關問題