2014-10-16 44 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 

在未來,總是張貼整個堆棧跟蹤和方法有關。猜測發生了什麼並不總是這麼容易。

相關問題