NameError:未定义的局部变量或方法`request' for HomeController:Class

时间:2015-04-04 19:57:40

标签: ruby-on-rails

这让我疯了。我使用的是Rails 3.2.17。我似乎无法在HomeController中使用Rails请求对象。

我的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

使用Pry控制台输出:

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 个答案:

答案 0 :(得分:1)

binding.pry语句似乎超出了您粘贴的输出中的refresh_blog方法AFAICT。

request类上不存在HomeController,但仅HomeController个对象上存在{/ p}}