2014-10-16 42 views
1
def index 
    @posts = Post.published 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @posts } 
     format.atom 
    end 
    end 

我正在接受这个错误。而且我是新的RoR任何人都可以帮助我。现在可以做什么?未定义的方法`调用'为#<ActiveRecord :: Relation []>

+2

你可以显示错误的回溯? – 2014-10-16 20:45:38

+0

app/controllers/posts_controller.rb:3:在'index' – mkayaa93 2014-10-16 20:47:42

+0

是发布了一个方法还是关系? – Anthony 2014-10-16 20:48:23

回答

6

您已经定义了一个范围,但给它一个关系而不是一个proc。你可能有这样的事情:

class Post < ActiveRecord::Base 
    scope :published, where(published: true) 
end 

更改它以这样的:

class Post < ActiveRecord::Base 
    scope :published, -> { where(published: true) } 
end 

在未来,总是张贴整个堆栈跟踪和方法有关。猜测发生了什么并不总是这么容易。

相关问题